cog/FileTree/PathNode.m

193 lines
3.2 KiB
Matlab
Raw Normal View History

2006-09-02 16:09:20 +00:00
//
// Node.m
// Cog
//
// Created by Vincent Spader on 8/20/2006.
// Copyright 2006 Vincent Spader. All rights reserved.
//
#import "PathNode.h"
#import "CogAudio/AudioPlayer.h"
#import "FileTreeDataSource.h"
2008-02-17 18:59:54 +00:00
#import "FileNode.h"
#import "DirectoryNode.h"
#import "SmartFolderNode.h"
#import "ContainerNode.h"
2006-09-02 16:09:20 +00:00
@implementation PathNode
2007-10-15 23:37:49 +00:00
//From http://developer.apple.com/documentation/Cocoa/Conceptual/LowLevelFileMgmt/Tasks/ResolvingAliases.html
NSURL *resolveAliases(NSURL *url)
2007-10-15 23:37:49 +00:00
{
FSRef fsRef;
if (CFURLGetFSRef((CFURLRef)url, &fsRef))
2007-10-15 23:37:49 +00:00
{
Boolean targetIsFolder, wasAliased;
2007-10-15 23:37:49 +00:00
if (FSResolveAliasFile (&fsRef, true /*resolveAliasChains*/, &targetIsFolder, &wasAliased) == noErr && wasAliased)
{
CFURLRef resolvedUrl = CFURLCreateFromFSRef(NULL, &fsRef);
if (resolvedUrl != NULL)
2007-10-15 23:37:49 +00:00
{
NSLog(@"Resolved...");
return [(NSURL *)resolvedUrl autorelease];
2007-10-15 23:37:49 +00:00
}
}
}
NSLog(@"Not resolved");
return url;
2007-10-15 23:37:49 +00:00
}
- (id)initWithDataSource:(FileTreeDataSource *)ds url:(NSURL *)u
2006-09-02 16:09:20 +00:00
{
self = [super init];
if (self)
{
dataSource = ds;
[self setURL: u];
2006-09-02 16:09:20 +00:00
}
return self;
}
2008-02-17 18:59:54 +00:00
- (void)setURL:(NSURL *)u
{
[u retain];
[url release];
url = u;
[display release];
display = [[NSFileManager defaultManager] displayNameAtPath:[url path]];
[display retain];
[icon release];
icon = [[NSWorkspace sharedWorkspace] iconForFile:[url path]];
[icon retain];
[icon setSize: NSMakeSize(16.0, 16.0)];
}
- (NSURL *)URL
2006-09-02 16:09:20 +00:00
{
return url;
2006-09-02 16:09:20 +00:00
}
- (void)updatePath
{
}
- (void)processPaths: (NSArray *)contents
{
NSMutableArray *newSubpaths = [[NSMutableArray alloc] init];
NSEnumerator *e = [contents objectEnumerator];
NSString *s;
while ((s = [e nextObject]))
{
if ([s characterAtIndex:0] == '.')
{
continue;
}
NSURL *u = [NSURL fileURLWithPath:s];
PathNode *newNode;
NSLog(@"Before: %@", u);
u = resolveAliases(u);
NSLog(@"After: %@", u);
2007-10-16 01:27:15 +00:00
if ([[s pathExtension] caseInsensitiveCompare:@"savedSearch"] == NSOrderedSame)
{
2007-10-15 23:18:58 +00:00
NSLog(@"Smart folder!");
newNode = [[SmartFolderNode alloc] initWithDataSource:dataSource url:u];
}
else
{
BOOL isDir;
[[NSFileManager defaultManager] fileExistsAtPath:[u path] isDirectory:&isDir];
2007-10-16 01:27:15 +00:00
if (!isDir && ![[AudioPlayer fileTypes] containsObject:[[[u path] pathExtension] lowercaseString]])
{
continue;
}
if (isDir)
{
newNode = [[DirectoryNode alloc] initWithDataSource:dataSource url:u];
}
else if ([[AudioPlayer containerTypes] containsObject:[[[u path] pathExtension] lowercaseString]])
{
newNode = [[ContainerNode alloc] initWithDataSource:dataSource url:u];
}
else
{
newNode = [[FileNode alloc] initWithDataSource:dataSource url:u];
}
}
[newSubpaths addObject:newNode];
[newNode release];
}
[self setSubpaths:newSubpaths];
[newSubpaths release];
}
- (NSArray *)subpaths
2006-09-02 16:09:20 +00:00
{
if (subpaths == nil)
{
[self updatePath];
}
return subpaths;
2006-09-02 16:09:20 +00:00
}
- (void)setSubpaths:(NSArray *)s
2006-09-02 16:09:20 +00:00
{
[s retain];
[subpaths release];
subpaths = s;
2006-09-02 16:09:20 +00:00
}
- (BOOL)isLeaf
{
return YES;
}
- (NSString *)display
{
return display;
}
- (NSImage *)icon
{
return icon;
}
- (void)dealloc
{
[url release];
[icon release];
[subpaths release];
[super dealloc];
}
2006-09-02 16:09:20 +00:00
@end