Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 896 lines (764 sloc) 32.201 kb
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 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
#import "QSObject_FileHandling.h"
#import "QSObject_Pasteboard.h"
#import "QSParser.h"
#import "QSComputerSource.h"
#import "QSResourceManager.h"
#import "QSLibrarian.h"
#import "QSTypes.h"
#import "QSRegistry.h"
#import <QSFoundation/QSFoundation.h>
#import "QSPreferenceKeys.h"
#import "NDResourceFork.h"
#import "QSCatalogEntry.h"
#import "NDAlias+QSMods.h"
#import "QSUTI.h"
#import "QSExecutor.h"
#import "QSMacros.h"
#import "QSAction.h"
#import "QSObject_PropertyList.h"
#include "QSLocalization.h"

#import "NSApplication_BLTRExtensions.h"


// Ankur, 21 Dec 07: 'useSmallIcons' not used anywhere. Commented out.
// Ankur, 12 Feb 08: as above for 'applicationIcons'

NSString *identifierForPaths(NSArray *paths) {
if ([paths count] == 1) return [paths lastObject];
return [paths componentsJoinedByString:@" "];
}

static NSDictionary *bundlePresetChildren;
//static BOOL useSmallIcons = NO;

NSArray *recentDocumentsForBundle(NSString *bundleIdentifier) {
    if (bundleIdentifier == nil) {
return nil;
}

// make sure latest changes are available
CFPreferencesSynchronize((CFStringRef) [bundleIdentifier stringByAppendingString:@".LSSharedFileList"],
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
NSDictionary *recentDocuments106 = [(NSDictionary *)CFPreferencesCopyValue((CFStringRef) @"RecentDocuments",
(CFStringRef) [bundleIdentifier stringByAppendingString:@".LSSharedFileList"],
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost) autorelease];
NSArray *recentDocuments = [recentDocuments106 objectForKey:@"CustomListItems"];

NSMutableArray *documentsArray = [NSMutableArray arrayWithCapacity:0];
NSData *bookmarkData;
NSURL *url;
NSError *err;
for(NSDictionary *documentStorage in recentDocuments) {
bookmarkData = [documentStorage objectForKey:@"Bookmark"];
err = nil;
url = [NSURL URLByResolvingBookmarkData:bookmarkData
options:NSURLBookmarkResolutionWithoutMounting|NSURLBookmarkResolutionWithoutUI
relativeToURL:nil
bookmarkDataIsStale:NO
error:&err];
if (url == nil || err != nil) {
// couldn't resolve bookmark, so skip
continue;
}
[documentsArray addObject:[url path]];
}
return documentsArray;
}

@interface QSFileSystemObjectHandler (hidden)
-(NSImage *)prepareImageforIcon:(NSImage *)icon;

@end

#pragma mark QSFileSystemObjectHandler

@implementation QSFileSystemObjectHandler

// !!! Andre Berg 20091017: Not so good to disable init when subclassing... re-enabling.

// Object Handler Methods
// +(void)initialize {
// useSmallIcons = [[NSUserDefaults standardUserDefaults] boolForKey:kUseSmallIcons];
// }
#if 1
- (id)init {
self = [super init];
if (self != nil) {
applicationIcons = [[NSMutableDictionary alloc] init];
}
return self;
}
- (NSMutableDictionary *)applicationIcons {
return applicationIcons;
}
#endif

- (QSObject *)parentOfObject:(QSObject *)object {
QSObject * parent = nil;
if ([object singleFilePath]) {
if ([[object singleFilePath] isEqualToString:@"/"]) parent = [QSProxyObject proxyWithIdentifier:@"QSComputerProxy"];
else parent = [QSObject fileObjectWithPath:[[object singleFilePath] stringByDeletingLastPathComponent]];
}
return parent;
}

- (id)dataForObject:(QSObject *)object pasteboardType:(NSString *)type {
return [object arrayForType:type];
}

- (BOOL)drawIconForObject:(QSObject *)object inRect:(NSRect)rect flipped:(BOOL)flipped {
if (NSWidth(rect) <= 32)
return NO;
NSString *path = [object singleFilePath];
    
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil]) {
LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path] , kLSRequestBasicFlagsOnly, &infoRec);
if (infoRec.flags & kLSItemInfoIsApplication) {
NSString *bundleIdentifier = [[NSBundle bundleWithPath:path] bundleIdentifier];

NSString *handlerName = [[QSReg tableNamed:@"QSBundleDrawingHandlers"] objectForKey:bundleIdentifier];
if (handlerName) {
id handler = [QSReg getClassInstance:handlerName];
if (handler) {
if ([handler respondsToSelector:@selector(drawIconForObject:inRect:flipped:)])
return [handler drawIconForObject:(QSObject *)object inRect:(NSRect)rect flipped:(BOOL)flipped];
return NO;
}
}
}
}
  return NO;
  
if (!path || [[path pathExtension] caseInsensitiveCompare:@"prefpane"] != NSOrderedSame)
return NO;

NSImage *image = [NSImage imageNamed:@"PrefPaneTemplate"];

[image setSize:[[image bestRepresentationForSize:rect.size] size]];
//[image adjustSizeToDrawAtSize:rect.size];
[image drawInRect:rect fromRect:rectFromSize([image size]) operation:NSCompositeSourceOver fraction:1.0f];

if ([object iconLoaded]) {
NSImage *cornerBadge = [object icon];
if (cornerBadge != image) {

NSRect badgeRect = NSMakeRect(16+48+rect.origin.x, 16+36+rect.origin.y, 32, 32);
NSImageRep *bestBadgeRep = [cornerBadge bestRepresentationForSize:badgeRect.size];

[cornerBadge setSize:[bestBadgeRep size]];

[[NSColor colorWithDeviceWhite:1.0 alpha:0.8] set];
//NSRectFillUsingOperation(NSInsetRect(badgeRect, -14, -14), NSCompositeSourceOver);
NSBezierPath *path = [NSBezierPath bezierPath];
[path appendBezierPathWithRoundedRectangle:NSInsetRect(badgeRect, -10, -10) withRadius:4];

[[NSColor colorWithDeviceWhite:1.0 alpha:1.0] setFill];
[[NSColor colorWithDeviceWhite:0.75 alpha:1.0] setStroke];
[path fill];
[path stroke];

NSFrameRectWithWidth(NSInsetRect(badgeRect, -5, -5), 2);

[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationNone];
[cornerBadge drawInRect:badgeRect fromRect:rectFromSize([cornerBadge size]) operation:NSCompositeSourceOver fraction:1.0];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
}
}
return YES;
}

- (NSString *)kindOfObject:(QSObject *)object {
NSString *path = [object singleFilePath];
LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path] , kLSRequestBasicFlagsOnly, &infoRec);
if (infoRec.flags & kLSItemInfoIsApplication) {
LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path] , kLSRequestBasicFlagsOnly, &infoRec);
if (infoRec.flags & kLSItemInfoIsApplication) {
return @"QSKindApplication";
}
}
return nil;
}

- (NSString *)detailsOfObject:(QSObject *)object {
NSArray *theFiles = [object arrayForType:QSFilePathType];
if ([theFiles count] == 1) {
NSString *path = [theFiles lastObject];
if ([path hasPrefix:NSTemporaryDirectory()])
return [@"(Quicksilver) " stringByAppendingPathComponent:[path lastPathComponent]];
else
return [path stringByAbbreviatingWithTildeInPath];
} else if ([theFiles count] >1) {
return [[theFiles arrayByPerformingSelector:@selector(lastPathComponent)] componentsJoinedByString:@", "];
}
return nil;
}

- (void)setQuickIconForObject:(QSObject *)object {
NSString *path = [object singleFilePath];
BOOL isDirectory;
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory];
if (isDirectory)
if ([[path pathExtension] isEqualToString:@"app"] || [[NSArray arrayWithObject:@"'APPL'"] containsObject:NSHFSTypeOfFile(path)])
[object setIcon:[QSResourceManager imageNamed:@"GenericApplicationIcon"]];
else
[object setIcon:[QSResourceManager imageNamed:@"GenericFolderIcon"]];
else
[object setIcon:[QSResourceManager imageNamed:@"UnknownFSObjectIcon"]];
}

- (BOOL)loadIconForObject:(QSObject *)object {
NSImage *theImage = nil;
NSArray *theFiles = [object arrayForType:QSFilePathType];
if (!theFiles) return NO;
if ([theFiles count] == 1) {
// it's a single file
// use basic file type icon temporarily
theImage = [[NSWorkspace sharedWorkspace] iconForFile:[theFiles lastObject]];
} else {
// it's a combined object, containing multiple files
NSMutableSet *set = [NSMutableSet set];
NSWorkspace *w = [NSWorkspace sharedWorkspace];
NSFileManager *manager = [NSFileManager defaultManager];
for(NSString *theFile in theFiles) {
NSString *type = [manager typeOfFile:theFile];
[set addObject:type?type:@"'msng'"];
}

if ([set containsObject:@"'fold'"]) {
[set removeObject:@"'fold'"];
[set addObject:@"'fldr'"];
}

if ([set count] == 1) {
theImage = [w iconForFileType:[set anyObject]];
} else {
theImage = [w iconForFiles:theFiles];
}
}

// set temporary image until preview icon is generated
theImage = [self prepareImageforIcon:theImage];
[object setIcon:theImage];

// if it's a single file, try to create preview icon
// this has to be started after the temporary icon is set, so the preview icon
// wont be overwritten by the temporary icon
if ([theFiles count] == 1) {
// do complicated preview icon loading in separate thread
NSInvocationOperation *theOp = [[[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(previewIcon:)
object:object] autorelease];
[[[QSLibrarian sharedInstance] previewImageQueue] addOperation:theOp];
}
return YES;
}

-(void)previewIcon:(id)object {
NSImage *theImage = nil;
NSArray *theFiles = [object arrayForType:QSFilePathType];
NSString *path = [theFiles lastObject];
NSString *firstFile = [theFiles objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];

// the object isn't a file/doesn't exist, so return. shouldn't actually happen
if (![manager fileExistsAtPath:path]) {
return;
}
LSItemInfoRecord infoRec;
//OSStatus status=
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path] , kLSRequestBasicFlagsOnly, &infoRec);

// try preview icon
if (!theImage && [[NSUserDefaults standardUserDefaults] boolForKey:@"QSLoadImagePreviews"]) {
// do preview icon loading in separate thread (using NSOperationQueue)
theImage = [NSImage imageWithPreviewOfFileAtPath:path ofSize:QSMaxIconSize asIcon:YES];
}

// Just for prefpanes?
if (!theImage && infoRec.flags & kLSItemInfoIsPackage) {
NSBundle *bundle = [NSBundle bundleWithPath:firstFile];
NSString *bundleImageName = nil;
if ([[firstFile pathExtension] isEqualToString:@"prefPane"]) {
bundleImageName = [[bundle infoDictionary] objectForKey:@"NSPrefPaneIconFile"];

if (!bundleImageName) bundleImageName = [[bundle infoDictionary] objectForKey:@"CFBundleIconFile"];
if (bundleImageName) {
NSString *bundleImagePath = [bundle pathForResource:bundleImageName ofType:nil];
theImage = [[[NSImage alloc] initWithContentsOfFile:bundleImagePath] autorelease];
}
}
}

// try QS's own methods to generate a preview
if (!theImage && [[NSUserDefaults standardUserDefaults] boolForKey:@"QSLoadImagePreviews"]) {
NSString *type = [manager typeOfFile:path];
if ([[NSImage imageUnfilteredFileTypes] containsObject:type])
theImage = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
else {
id provider = [QSReg instanceForKey:type inTable:@"QSFSFileTypePreviewers"];
//NSLog(@"provider %@", [QSReg tableNamed:@"QSFSFileTypePreviewers"]);
theImage = [provider iconForFile:path ofType:type];
}
}

// fallback, if no of the other methods worked: just use icon for filetype
if (!theImage) {
theImage = [[NSWorkspace sharedWorkspace] iconForFile:path];
}

theImage = [self prepareImageforIcon:theImage];

[object setIcon:theImage];
[[NSNotificationCenter defaultCenter] postNotificationName:QSObjectIconModified object:object];
}

-(NSImage *)prepareImageforIcon:(NSImage *)theImage {
// last fallback, other methods didn't work
if (!theImage) theImage = [QSResourceManager imageNamed:@"GenericQuestionMarkIcon"];

// make sure image is present in the correct sizes
if (theImage) {
[theImage createRepresentationOfSize:NSMakeSize(32, 32)];
[theImage createRepresentationOfSize:NSMakeSize(16, 16)];
}

// remove all image representations that are larger then QSMaxIconSize
// not really sure if this is needed or even makes sense
// but it was in here before, but only removing exactly the
// 128x128 representation, if QSMaxIconSize was smaller than 128x128
// and there was a warning-comment: "***warning * use this better"
for (NSImageRep *imgRep in [theImage representations]) {
if ([imgRep size].width > QSMaxIconSize.width) {
[theImage removeRepresentation:imgRep];
}
}

return theImage;
}

- (BOOL)objectHasChildren:(QSObject *)object {
BOOL isDirectory;
NSString *path = [object singleFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) {
        
LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path] , kLSRequestBasicFlagsOnly, &infoRec);
        
        // A plain folder (not a package) has children
        if (isDirectory && !(infoRec.flags & kLSItemInfoIsPackage)) {
            return YES;
        }

        // If it's an app check to see if there's a handler for it (e.g. a plugin) or if there are recent documents
if (infoRec.flags & kLSItemInfoIsApplication) {
NSString *bundleIdentifier = [[NSBundle bundleWithPath:path] bundleIdentifier];
            
            // Does the app have an external handler? (e.g. a plugin)
NSString *handlerName = [[QSReg tableNamed:@"QSBundleChildHandlers"] objectForKey:bundleIdentifier];
if (handlerName) {
                return YES;
            }
            // Does the app have valid recent documents
            if (bundleIdentifier) {
                NSDictionary *recentDocuments = (NSDictionary *)CFPreferencesCopyValue((CFStringRef) @"RecentDocuments",
                                                                                       (CFStringRef) [bundleIdentifier stringByAppendingString:@".LSSharedFileList"],
                                                                                       kCFPreferencesCurrentUser,
                                                                                       kCFPreferencesAnyHost);
                if (recentDocuments) {
                    NSArray *recentDocumentsArray = [recentDocuments objectForKey:@"CustomListItems"];
                    [recentDocuments release];
                    if (recentDocumentsArray && [recentDocumentsArray count]) {
                        return YES;
                    }
                }
            }
}
        
        // If there is a valid file parser (text or HTML parser) the object has children
        NSString *uti = QSUTIWithLSInfoRec(path, &infoRec);
        id <QSParser> parser = [QSReg instanceForKey:uti inTable:@"QSFSFileTypeParsers"];
        if (parser) {
            return YES;
        }
        
        // An alias has children (the resolved file)
if (infoRec.flags & kLSItemInfoIsAliasFile) {
            return YES;
        }
}
    
return NO;

}
- (BOOL)objectHasValidChildren:(QSObject *)object {
if ([object fileCount] == 1) {
NSString *path = [object singleFilePath];

LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path] , kLSRequestBasicFlagsOnly, &infoRec);
if (infoRec.flags & kLSItemInfoIsApplication) {
NSString *bundleIdentifier = [[NSBundle bundleWithPath:path] bundleIdentifier];
NSString *handlerName = [[QSReg tableNamed:@"QSBundleChildHandlers"] objectForKey:bundleIdentifier];
if (handlerName) {
id handler = [QSReg getClassInstance:handlerName];
if (handler) {
if ([handler respondsToSelector:@selector(objectHasValidChildren:)])
return [handler objectHasValidChildren:object];
return NO;
}
}
return YES;
}

NSTimeInterval modDate = [[[[NSFileManager defaultManager] attributesOfItemAtPath:path error:NULL] fileModificationDate] timeIntervalSinceReferenceDate];
if (modDate>[object childrenLoadedDate]) return NO;
}
return YES;

}

- (NSDragOperation) operationForDrag:(id <NSDraggingInfo>)sender ontoObject:(QSObject *)dObject withObject:(QSBasicObject *)iObject {
if (![iObject arrayForType:QSFilePathType])
return 0;
if ([dObject fileCount] >1)
return NSDragOperationGeneric;
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
if ([dObject isApplication])
return NSDragOperationPrivate;
else if ([dObject isFolder]) {
NSDragOperation defaultOp = [[NSFileManager defaultManager] defaultDragOperationForMovingPaths:[iObject validPaths] toDestination:[dObject singleFilePath]];
if (defaultOp == NSDragOperationMove) {
if (sourceDragMask&NSDragOperationMove)
return NSDragOperationMove;
else if (sourceDragMask&NSDragOperationCopy)
return NSDragOperationCopy;
} else if (defaultOp == NSDragOperationCopy)
return NSDragOperationCopy;
}
return sourceDragMask&NSDragOperationGeneric;
}
- (NSString *)actionForDragMask:(NSDragOperation)operation ontoObject:(QSObject *)dObject withObject:(QSBasicObject *)iObject {
if ([dObject fileCount] >1)
return 0;
if ([dObject isApplication]) {
return @"FileOpenWithAction";
} else if ([dObject isFolder]) {
if (operation&NSDragOperationMove)
return @"FileMoveToAction";
else if (operation&NSDragOperationCopy)
return @"FileCopyToAction";
}
return 0;
}

- (NSAppleEventDescriptor *)AEDescriptorForObject:(QSObject *)object {
return [NSAppleEventDescriptor aliasListDescriptorWithArray:[object validPaths]];
}

- (NSString *)identifierForObject:(QSObject *)object {
return identifierForPaths([object arrayForType:QSFilePathType]);
}
- (BOOL)loadChildrenForObject:(QSObject *)object {
NSArray *newChildren = nil;
NSArray *newAltChildren = nil;

if ([object fileCount] == 1) {
NSString *path = [object singleFilePath];
if (![path length]) return NO;
BOOL isDirectory;
NSFileManager *manager = [NSFileManager defaultManager];

LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path], kLSRequestAllInfo, &infoRec);
        [(NSString*)infoRec.extension autorelease];

if (infoRec.flags & kLSItemInfoIsAliasFile) {
path = [manager resolveAliasAtPath:path];
if ([manager fileExistsAtPath:path isDirectory:&isDirectory] && !isDirectory) {
[object setChildren:[NSArray arrayWithObject:[QSObject fileObjectWithPath:path]]];
return YES;
}
}

NSMutableArray *fileChildren = [NSMutableArray arrayWithCapacity:1];
NSMutableArray *visibleFileChildren = [NSMutableArray arrayWithCapacity:1];

// NSString *file;
// NSEnumerator *enumerator = [[manager contentsOfDirectoryAtPath:path error:nil] objectEnumerator];
// while (file = [enumerator nextObject]) {
// file = [path stringByAppendingPathComponent:file];
// [fileChildren addObject:file];
// if ([manager isVisible:file])
// [visibleFileChildren addObject:file];
// }
        
        NSArray * dirContents = [manager contentsOfDirectoryAtPath:path error:nil];
for(NSString * file in dirContents) {
file = [path stringByAppendingPathComponent:file];
[fileChildren addObject:file];
if ([manager isVisible:file])
[visibleFileChildren addObject:file];
}

newChildren = [QSObject fileObjectsWithPathArray:visibleFileChildren];
newAltChildren = [QSObject fileObjectsWithPathArray:fileChildren];

if (newAltChildren) [object setAltChildren:newAltChildren];

if (infoRec.flags & kLSItemInfoIsApplication) {
// ***warning * omit other types of bundles
//newChildren = nil;

NSString *bundleIdentifier = [[NSBundle bundleWithPath:path] bundleIdentifier];
NSString *handlerName = [[QSReg tableNamed:@"QSBundleChildHandlers"] objectForKey:bundleIdentifier];
id handler = nil;

if (handlerName) handler = [QSReg getClassInstance:handlerName];

if (handler) {
return [handler loadChildrenForObject:object];
} else {
if (!bundlePresetChildren) {
bundlePresetChildren = [QSReg tableNamed:@"QSBundleChildPresets"];
//[[NSDictionary dictionaryWithContentsOfFile:
// [[NSBundle mainBundle] pathForResource:@"BundleChildPresets" ofType:@"plist"]]retain];
}

NSString *childPreset = [bundlePresetChildren objectForKey:bundleIdentifier];
if (childPreset) {
#ifdef DEBUG
if (VERBOSE) NSLog(@"using preset %@", childPreset);
#endif
QSCatalogEntry *theEntry = [[QSLibrarian sharedInstance] entryForID:childPreset];
newChildren = [theEntry contentsScanIfNeeded:YES];
} else {
NSArray *recentDocuments = recentDocumentsForBundle(bundleIdentifier);
newChildren = [QSObject fileObjectsWithPathArray:recentDocuments];

for(QSObject * child in newChildren) {
[child setObject:bundleIdentifier forMeta:@"QSPreferredApplication"];
}
}
}

} else if ((infoRec.flags & kLSItemInfoIsPackage) || !(infoRec.flags & kLSItemInfoIsContainer) ) {
//NSString *type = [[NSFileManager defaultManager] typeOfFile:path];

NSString *uti = QSUTIWithLSInfoRec(path, &infoRec);
            
id <QSParser> parser = [QSReg instanceForKey:uti inTable:@"QSFSFileTypeParsers"];
NSArray *children = [parser objectsFromPath:path withSettings:nil];
if (children) {
[object setChildren:children];
return YES;
}

}

} else {
newChildren = [QSObject fileObjectsWithPathArray:[object arrayForType:QSFilePathType]];
}

if (newChildren) [object setChildren:newChildren];
    
return YES;
}

- (NSArray *)actionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject {
    NSString *path = [dObject objectForType:QSFilePathType];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil]) {
        LSItemInfoRecord infoRec;
        LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path], kLSRequestBasicFlagsOnly, &infoRec);
        if (infoRec.flags & kLSItemInfoIsApplication) {
            NSMutableArray *actions = (NSMutableArray *)[QSExec validActionsForDirectObject:dObject indirectObject:iObject];
            NSString *bundleIdentifier = [[NSBundle bundleWithPath:path] bundleIdentifier];

            //NSLog(@"actions %d", [actions count]);
            NSDictionary *appActions = [[QSReg tableNamed:@"QSApplicationActions"] objectForKey:bundleIdentifier];
if([appActions count]) {
            for(NSString *actionID in appActions) {
NSDictionary *actionDict = [appActions objectForKey:actionID];
                actionDict = [[actionDict copy] autorelease];
                [actions addObject:[QSAction actionWithDictionary:actionDict identifier:actionID]];
            }
}
            // NSLog(@"actions %d", [actions count]);

            return actions;

        }
    }
    return nil;
}

@end

@implementation QSBasicObject (FileHandling)

- (NSString *)singleFilePath {return [self objectForType:QSFilePathType];
}

- (NSString *)validSingleFilePath {
NSString *path = [self objectForType:QSFilePathType];
if (path && [[NSFileManager defaultManager] fileExistsAtPath:path])
return path;
return nil;
}

- (NSArray *)validPaths {return [self validPathsResolvingAliases:NO];}
- (NSArray *)validPathsResolvingAliases:(BOOL)resolve {
NSArray *paths = [self arrayForType:QSFilePathType];
NSFileManager *manager = [NSFileManager defaultManager];
BOOL exists = [manager filesExistAtPaths:(NSArray *)paths];
if (exists) return paths;

if ([paths count] == 1) {
NSString *aliasFile = [self objectForType:QSAliasFilePathType];
if ([manager fileExistsAtPath:aliasFile]) {
#ifdef DEBUG
if (VERBOSE) NSLog(@"Using original alias file:%@", aliasFile);
#endif
return [NSArray arrayWithObject:aliasFile];
}
}
return nil;
}

- (NSInteger) fileCount {
return [[self arrayForType:QSFilePathType] count];
}

@end

@implementation QSObject (FileHandling)

+ (QSObject *)fileObjectWithPath:(NSString *)path {
if (![path length])
return nil;
path = [path stringByStandardizingPath];
if ([[path pathExtension] isEqualToString:@"silver"])
return [QSObject objectWithDictionary:[NSDictionary dictionaryWithContentsOfFile:path]];

QSObject *newObject = [QSObject objectWithIdentifier:path];
if (!newObject) {
newObject = [[[QSObject alloc] initWithArray:[NSArray arrayWithObject:path]] autorelease];
}
if ([clippingTypes containsObject:[[NSFileManager defaultManager] typeOfFile:path]])
[newObject performSelectorOnMainThread:@selector(addContentsOfClipping:) withObject:path waitUntilDone:YES];
return newObject;
}

+ (QSObject *)fileObjectWithFileURL:(NSURL *)fileURL {
    return [self fileObjectWithPath:[fileURL path]];
}

+ (QSObject *)fileObjectWithArray:(NSArray *)paths {
QSObject *newObject = [QSObject objectByMergingObjects:[self fileObjectsWithPathArray:paths]];
if (!newObject) {
if ([paths count] > 1)
newObject = [[[QSObject alloc] initWithArray:paths] autorelease];
else if ([paths count] == 0)
return nil;
else
newObject = [QSObject fileObjectWithPath:[paths lastObject]];
}
return newObject;
}

+ (NSArray *)fileObjectsWithPathArray:(NSArray *)pathArray {
NSMutableArray *fileObjectArray = [NSMutableArray arrayWithCapacity:1];
id object;
for (id loopItem in pathArray) {
if (object = [QSObject fileObjectWithPath:loopItem])
[fileObjectArray addObject:object];
}
return fileObjectArray;
}

+ (NSMutableArray *)fileObjectsWithURLArray:(NSArray *)pathArray {
NSMutableArray *fileObjectArray = [NSMutableArray arrayWithCapacity:[pathArray count]];
for (id loopItem in pathArray) {
[fileObjectArray addObject:[QSObject fileObjectWithPath:[loopItem path]]];
}
return fileObjectArray;
}

- (id)initWithArray:(NSArray *)paths {
NSString *thisIdentifier = identifierForPaths(paths);

// return an already-created object if it exists
QSObject *existingObject = [QSObject objectWithIdentifier:thisIdentifier];
if (existingObject) {
[existingObject retain];
return existingObject;
}

// if no previous object has been created, then create a new one
if (self = [self init]) {
if ([paths count] == 1) {
NSString *path = [paths lastObject];
[[self dataDictionary] setObject:path forKey:QSFilePathType];
NSString *uti = QSUTIOfFile(path);
id handler = [QSReg instanceForKey:uti inTable:@"QSFileObjectCreationHandlers"];
if (handler) {
// fheckl 2011-02-25
// XCode CLang analysis: incorrect decrement of reference count
// because method name starts with init -> no problem
[handler initFileObject:self ofType:uti];
return self;
}
} else {
[[self dataDictionary] setObject:paths forKey:QSFilePathType];
}
[QSObject registerObject:self withIdentifier:thisIdentifier];
[self setPrimaryType:QSFilePathType];
[self getNameFromFiles];
}
return self;
}

// Checks to see if the object in question is an application
- (BOOL)isApplication {
NSString *path = [self singleFilePath];
    if(!path) {
        return NO;
    }
LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path], kLSRequestBasicFlagsOnly, &infoRec);
return (infoRec.flags & kLSItemInfoIsApplication);
}

- (BOOL)isFolder {
BOOL isDirectory;
return ([[NSFileManager defaultManager] fileExistsAtPath:[self singleFilePath] isDirectory:&isDirectory]) ? isDirectory : NO;
}

- (NSString *)localizedPrefPaneKind {
static NSString *prefPaneKindString = nil;
if (!prefPaneKindString)
prefPaneKindString = [[[NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.apple.systempreferences"]] localizedStringForKey:@"PREF_PANE" value:@" Preferences" table:nil] retain];
return prefPaneKindString;
}

- (NSString *)bundleNameFromInfoDict:(NSDictionary *)infoDict {
    // Use the display name
    return [infoDict objectForKey:@"CFBundleDisplayName"];
}

- (NSString *)descriptiveNameForPackage:(NSString *)path withKindSuffix:(BOOL)includeKind {
    NSURL *fileURL = [NSURL fileURLWithPath:path];

    NSString *bundleName = nil;
    // First try the localised info Dict
    NSDictionary *infoDict = [[NSBundle bundleWithURL:fileURL] localizedInfoDictionary];
    if (infoDict) {
        bundleName = [self bundleNameFromInfoDict:infoDict];
    }
    // Get the general info Dict
    if (!bundleName) {
        infoDict = [[NSBundle bundleWithURL:fileURL] infoDictionary];
        bundleName = [self bundleNameFromInfoDict:infoDict];
    }
    
    if (!bundleName) {
        return nil;
    }
    
if (includeKind) {
        NSString *kind = nil;
if ([[path pathExtension] caseInsensitiveCompare:@"prefPane"] == NSOrderedSame) {
kind = QSGetLocalizationStatus() ? [self localizedPrefPaneKind] : @"Preference Pane";
} else {
LSCopyKindStringForURL((CFURLRef)fileURL, (CFStringRef *)&kind);
      [kind autorelease];
}
#ifdef DEBUG
      if (DEBUG_LOCALIZATION) NSLog(@"kind: %@", kind);
#endif

        if ([kind length]) {
bundleName = [NSString stringWithFormat:@"%@ %@", bundleName, kind];
        }
    }
        
    bundleName = [[bundleName retain] autorelease];
    
return bundleName;
}

- (void)getNameFromFiles {
NSFileManager *manager = [[NSFileManager alloc] init];
NSString *newName = nil;
NSString *newLabel = nil;
if ([self count] >1) {
NSArray *paths = [self arrayForType:QSFilePathType];
NSString *container = [self filesContainer];
NSString *type = [self filesType];
BOOL onDesktop = [container isEqualToString:[@"~/Desktop/" stringByStandardizingPath]];
newName = [NSString stringWithFormat:@"%ld %@ %@ \"%@\"", (long)[paths count] , type, onDesktop?@"on":@"in", [container lastPathComponent]];
} else {
NSString *path = [self objectForType:QSFilePathType];

LSItemInfoRecord infoRec;
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:path] , kLSRequestBasicFlagsOnly, &infoRec);

if (infoRec.flags & kLSItemInfoIsPackage) {
newLabel = [self descriptiveNameForPackage:(NSString *)path withKindSuffix:!(infoRec.flags & kLSItemInfoIsApplication)];
if ([newLabel isEqualToString:newName]) newLabel = nil;
}
        // Fall back on using NSFileManager to get the name
if (!newName) {
newName = [[NSFileManager defaultManager] displayNameAtPath:path];
        }
        
if (!newLabel && ![self label]) {
newLabel = [manager displayNameAtPath:path];
if ([newName isEqualToString:newLabel]) newLabel = nil;
}
if ([path isEqualToString:@"/"]) newLabel = [manager displayNameAtPath:path];
}
    [manager release];
[self setName:newName];
[self setLabel:newLabel];
}

- (NSString *)filesContainer {
NSArray *paths = [self arrayForType:QSFilePathType];

NSString *commonPath = [[[paths objectAtIndex:0] stringByStandardizingPath] stringByDeletingLastPathComponent];
for (id loopItem in paths) {
NSString *thisPath = [loopItem stringByStandardizingPath];
while (commonPath && ![thisPath hasPrefix:commonPath])
commonPath = [commonPath stringByDeletingLastPathComponent];
}
return commonPath;
}

- (NSString *)singleFileType {
return [[NSFileManager defaultManager] typeOfFile:[self singleFilePath]];
}

- (NSString *)filesType {
BOOL appsOnly = YES;
BOOL foldersOnly = YES;
BOOL filesOnly = YES;
NSString *kind = nil;
NSArray *paths = [self arrayForType:QSFilePathType];

for (id loopItem in paths) {
NSString *thisPath = [loopItem stringByStandardizingPath];
NSString *type = [[NSFileManager defaultManager] typeOfFile:thisPath];
if ([type isEqualToString:@"'fold'"]) {
filesOnly = NO;
appsOnly = NO;
            // application type (or Finder 'FNDR')
} else if ([type isEqualToString:@"app"] || [type isEqualToString:@"'APPL'"] || [type isEqualToString:@"'FNDR'"]) {
foldersOnly = NO;
filesOnly = NO;
} else {
appsOnly = NO;
foldersOnly = NO;
// if (!kind) {
kind = [self kindOfFile:thisPath];
// } else if (![kind isEqualToString:[self kindOfFile:thisPath]]) {
// kind = @"";
// }
}
}
if (appsOnly) return @"Applications";
if (foldersOnly) return @"Folders";
if (filesOnly) {
if ([kind length]) return [NSString stringWithFormat:@"[%@] ", kind];
return @"Files";
}
return @"Items";
}

- (NSString *)kindOfFile:(NSString *)path {
NSString *kind;
return (!path || LSCopyKindStringForURL((CFURLRef) [NSURL fileURLWithPath:path], (CFStringRef *)&kind)) ? nil : [kind autorelease];
}

#if 0
- (QSObject *)fileObjectByMergingWith:(QSObject *)mergeObject {
// NSArray *moreFiles = [[mergeObject dataDictionary] objectForKey:QSFilePathType;
return nil;
}
#endif

@end
Something went wrong with that request. Please try again.