Cog/FileDrawer/PathNode.m

255 lines
5.3 KiB
Mathematica
Raw Normal View History

2006-09-02 12:09:20 -04: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"
#import "UKKQueue.h"
@class FileNode;
@class DirectoryNode;
@class SmartFolderNode;
2006-09-02 12:09:20 -04:00
@implementation PathNode
2007-10-15 20:37:49 -03:00
//From http://developer.apple.com/documentation/Cocoa/Conceptual/LowLevelFileMgmt/Tasks/ResolvingAliases.html
NSString *resolveAliases(NSString *path)
{
NSString *resolvedPath = nil;
CFURLRef url;
url = CFURLCreateWithFileSystemPath(NULL /*allocator*/, (CFStringRef)path, kCFURLPOSIXPathStyle, NO /*isDirectory*/);
if (url != NULL)
{
FSRef fsRef;
if (CFURLGetFSRef(url, &fsRef))
{
Boolean targetIsFolder, wasAliased;
if (FSResolveAliasFile (&fsRef, true /*resolveAliasChains*/, &targetIsFolder, &wasAliased) == noErr && wasAliased)
{
CFURLRef resolvedUrl = CFURLCreateFromFSRef(NULL, &fsRef);
if (resolvedUrl != NULL)
{
resolvedPath = (NSString*)
CFURLCopyFileSystemPath(resolvedUrl, kCFURLPOSIXPathStyle);
CFRelease(resolvedUrl);
}
}
}
CFRelease(url);
}
if (resolvedPath==nil)
resolvedPath = [[NSString alloc] initWithString:path];
return resolvedPath;
}
- (id)initWithDataSource:(FileTreeDataSource *)ds path:(NSString *)p
2006-09-02 12:09:20 -04:00
{
self = [super init];
if (self)
{
dataSource = ds;
[self setPath: p];
2006-09-02 12:09:20 -04:00
}
return self;
}
- (void)stopWatching
2006-09-02 12:09:20 -04:00
{
if (path)
{
NSLog(@"Stopped watching...: %@", path);
//Remove all in one go
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[[UKKQueue sharedFileWatcher] removePath:path];
}
}
- (void)startWatching
{
if (path)
{
NSLog(@"WATCHING! %@ %i", path, path);
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(updatePathNotification:) name:UKFileWatcherRenameNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(updatePathNotification:) name:UKFileWatcherWriteNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(updatePathNotification:) name:UKFileWatcherDeleteNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(updatePathNotification:) name:UKFileWatcherAttributeChangeNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(updatePathNotification:) name:UKFileWatcherSizeIncreaseNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(updatePathNotification:) name:UKFileWatcherLinkCountChangeNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(updatePathNotification:) name:UKFileWatcherAccessRevocationNotification object:nil];
[[UKKQueue sharedFileWatcher] addPath:path];
}
2006-09-02 12:09:20 -04:00
}
- (void)setPath:(NSString *)p
{
[p retain];
[self stopWatching];
[path release];
path = p;
[self startWatching];
[displayPath release];
displayPath = [[NSFileManager defaultManager] displayNameAtPath:path];
[displayPath retain];
[icon release];
icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
[icon retain];
[icon setSize: NSMakeSize(16.0, 16.0)];
}
2006-09-02 12:09:20 -04:00
- (NSString *)path
{
return path;
}
- (void)updatePath
{
}
- (void)updatePathNotification:(NSNotification *)notification
{
[self performSelectorOnMainThread:@selector(updatePathNotificationMainThread:) withObject:notification waitUntilDone:YES];
}
- (void)updatePathNotificationMainThread:(NSNotification *)notification
{
NSString *p = [[notification userInfo] objectForKey:@"path"];
if (p == path)
{
NSLog(@"Update path notification: %@", [NSThread currentThread]);
[self updatePath];
[dataSource reloadPathNode:self];
}
}
- (void)processPaths: (NSArray *)contents
{
NSMutableArray *newSubpaths = [[NSMutableArray alloc] init];
NSEnumerator *e = [contents objectEnumerator];
NSString *s;
while ((s = [e nextObject]))
{
if ([s characterAtIndex:0] == '.')
{
continue;
}
PathNode *newNode;
2007-10-15 20:37:49 -03:00
s = resolveAliases(s);
if ([[s pathExtension] caseInsensitiveCompare:@"savedSearch"] == NSOrderedSame)
{
2007-10-15 20:18:58 -03:00
NSLog(@"Smart folder!");
newNode = [[SmartFolderNode alloc] initWithDataSource:dataSource path:s];
}
else
{
BOOL isDir;
2007-10-15 20:18:58 -03:00
[[NSFileManager defaultManager] fileExistsAtPath:s isDirectory:&isDir];
if (!isDir && ![[AudioPlayer fileTypes] containsObject:[s pathExtension]])
{
continue;
}
if (isDir)
{
2007-10-15 20:18:58 -03:00
newNode = [[DirectoryNode alloc] initWithDataSource:dataSource path: s];
}
else
{
2007-10-15 20:18:58 -03:00
newNode = [[FileNode alloc] initWithDataSource:dataSource path: s];
}
}
[newSubpaths addObject:newNode];
[newNode release];
}
[self setSubpaths:newSubpaths];
[newSubpaths release];
}
- (NSArray *)subpaths
2006-09-02 12:09:20 -04:00
{
if (subpaths == nil)
{
[self updatePath];
}
return subpaths;
2006-09-02 12:09:20 -04:00
}
- (void)setSubpaths:(NSArray *)s
2006-09-02 12:09:20 -04:00
{
[s retain];
[subpaths release];
subpaths = s;
2006-09-02 12:09:20 -04:00
}
- (BOOL)isLeaf
{
return YES;
}
- (NSString *)displayPath
{
return displayPath;
}
- (NSImage *)icon
{
return icon;
}
- (void)dealloc
{
[self stopWatching];
[path release];
[icon release];
[subpaths release];
[super dealloc];
}
2006-09-02 12:09:20 -04:00
@end