2007-10-18 02:52:03 +00:00
|
|
|
//
|
|
|
|
// FileTreeDataSource.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 10/14/07.
|
|
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "FileTreeDataSource.h"
|
|
|
|
|
|
|
|
#import "DNDArrayController.h"
|
|
|
|
|
|
|
|
#import "DirectoryNode.h"
|
2008-02-18 03:27:59 +00:00
|
|
|
#import "PathWatcher.h"
|
2007-10-18 02:52:03 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2007-10-18 02:52:03 +00:00
|
|
|
@implementation FileTreeDataSource
|
|
|
|
|
2008-02-18 01:46:34 +00:00
|
|
|
+ (void)initialize
|
2008-02-17 19:27:29 +00:00
|
|
|
{
|
|
|
|
NSMutableDictionary *userDefaultsValuesDict = [NSMutableDictionary dictionary];
|
|
|
|
|
2008-02-17 21:04:36 +00:00
|
|
|
[userDefaultsValuesDict setObject:[[NSURL fileURLWithPath:[@"~/Music" stringByExpandingTildeInPath]] absoluteString] forKey:@"fileTreeRootURL"];
|
2008-02-17 19:27:29 +00:00
|
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];
|
|
|
|
}
|
|
|
|
|
2007-10-18 02:52:03 +00:00
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
2008-02-18 01:46:34 +00:00
|
|
|
[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:@"values.fileTreeRootURL" options:0 context:nil];
|
|
|
|
|
2008-02-17 21:04:36 +00:00
|
|
|
[self setRootURL: [NSURL URLWithString:[[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"fileTreeRootURL"]]];
|
2007-10-18 02:52:03 +00:00
|
|
|
|
2009-08-16 16:49:34 +00:00
|
|
|
[pathControl setTarget:self];
|
|
|
|
[pathControl setAction:@selector(pathControlAction:)];
|
2008-02-20 19:36:34 +00:00
|
|
|
}
|
2008-02-20 23:22:39 +00:00
|
|
|
|
2008-02-17 19:27:29 +00:00
|
|
|
- (void) observeValueForKeyPath:(NSString *)keyPath
|
|
|
|
ofObject:(id)object
|
|
|
|
change:(NSDictionary *)change
|
|
|
|
context:(void *)context
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"File tree root URL: %@\n", [[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"fileTreeRootURL"]);
|
2008-02-17 21:04:36 +00:00
|
|
|
if ([keyPath isEqualToString:@"values.fileTreeRootURL"]) {
|
|
|
|
[self setRootURL:[NSURL URLWithString:[[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"fileTreeRootURL"]]];
|
2008-02-17 19:27:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-16 16:49:34 +00:00
|
|
|
- (void)changeURL:(NSURL *)url
|
|
|
|
{
|
|
|
|
if (url != nil)
|
|
|
|
{
|
|
|
|
[[[NSUserDefaultsController sharedUserDefaultsController] defaults] setObject:[url absoluteString] forKey:@"fileTreeRootURL"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)pathControlAction:(id)sender
|
|
|
|
{
|
|
|
|
if ([pathControl clickedPathComponentCell] != nil && [[pathControl clickedPathComponentCell] URL] != nil)
|
|
|
|
{
|
|
|
|
[self changeURL:[[pathControl clickedPathComponentCell] URL]];
|
|
|
|
}
|
|
|
|
}
|
2008-02-17 19:27:29 +00:00
|
|
|
|
2008-02-17 21:04:36 +00:00
|
|
|
- (NSURL *)rootURL
|
2008-01-31 00:50:50 +00:00
|
|
|
{
|
2008-02-20 01:01:15 +00:00
|
|
|
return [rootNode URL];
|
2008-01-31 00:50:50 +00:00
|
|
|
}
|
|
|
|
|
2008-02-17 21:04:36 +00:00
|
|
|
- (void)setRootURL: (NSURL *)rootURL
|
2007-10-18 02:52:03 +00:00
|
|
|
{
|
2015-02-09 02:15:42 +00:00
|
|
|
if (![[NSFileManager defaultManager] fileExistsAtPath:[rootURL path]])
|
|
|
|
rootURL = [NSURL fileURLWithPath:[@"~/Music" stringByExpandingTildeInPath]];
|
|
|
|
|
2008-02-17 21:04:36 +00:00
|
|
|
rootNode = [[DirectoryNode alloc] initWithDataSource:self url:rootURL];
|
2007-10-18 02:52:03 +00:00
|
|
|
|
2008-02-18 03:27:59 +00:00
|
|
|
[watcher setPath:[rootURL path]];
|
|
|
|
|
2007-10-18 02:52:03 +00:00
|
|
|
[self reloadPathNode:rootNode];
|
|
|
|
}
|
|
|
|
|
2008-02-18 03:27:59 +00:00
|
|
|
- (PathNode *)nodeForPath:(NSString *)path
|
|
|
|
{
|
2008-02-18 03:31:09 +00:00
|
|
|
NSString *relativePath = [[path stringByReplacingOccurrencesOfString:[[[self rootURL] path] stringByAppendingString:@"/"]
|
|
|
|
withString:@""
|
|
|
|
options:NSAnchoredSearch
|
|
|
|
range:NSMakeRange(0, [path length])
|
|
|
|
] stringByStandardizingPath];
|
|
|
|
PathNode *node = rootNode;
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Root | Relative | Path: %@ | %@ | %@",[[self rootURL] path], relativePath, path);
|
2008-03-01 15:04:46 +00:00
|
|
|
for (NSString *c in [relativePath pathComponents])
|
2008-02-18 03:27:59 +00:00
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"COMPONENT: %@", c);
|
2008-02-18 03:27:59 +00:00
|
|
|
BOOL found = NO;
|
2008-02-18 03:31:09 +00:00
|
|
|
for (PathNode *subnode in [node subpaths]) {
|
2008-02-20 01:01:15 +00:00
|
|
|
if ([[[[subnode URL] path] lastPathComponent] isEqualToString:c]) {
|
2008-02-18 03:31:09 +00:00
|
|
|
node = subnode;
|
2008-02-18 03:27:59 +00:00
|
|
|
found = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Not found!");
|
2008-02-18 03:27:59 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-18 03:31:09 +00:00
|
|
|
return node;
|
2008-02-18 03:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)pathDidChange:(NSString *)path
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"PATH DID CHANGE: %@", path);
|
2008-02-18 03:27:59 +00:00
|
|
|
//Need to find the corresponding node...and call [node reloadPath], then [self reloadPathNode:node]
|
|
|
|
PathNode *node = [self nodeForPath:path];
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"NODE IS: %@", node);
|
2008-02-18 03:27:59 +00:00
|
|
|
[node updatePath];
|
|
|
|
[self reloadPathNode:node];
|
|
|
|
}
|
|
|
|
|
2007-10-18 02:52:03 +00:00
|
|
|
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
|
|
|
|
{
|
|
|
|
PathNode *n = (item == nil ? rootNode : item);
|
|
|
|
|
2016-06-30 05:10:29 +00:00
|
|
|
return (int) [[n subpaths] count];
|
2007-10-18 02:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
|
|
|
|
{
|
|
|
|
PathNode *n = (item == nil ? rootNode : item);
|
|
|
|
|
|
|
|
return ([n isLeaf] == NO);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
|
|
|
|
{
|
|
|
|
PathNode *n = (item == nil ? rootNode : item);
|
|
|
|
|
|
|
|
return [[n subpaths] objectAtIndex:index];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
|
|
|
|
{
|
|
|
|
PathNode *n = (item == nil ? rootNode : item);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Drag it drop it
|
|
|
|
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray*)items toPasteboard:(NSPasteboard*)pboard {
|
|
|
|
//Get selected paths
|
|
|
|
NSMutableArray *urls = [NSMutableArray arrayWithCapacity:[items count]];
|
2009-08-16 16:49:34 +00:00
|
|
|
NSMutableArray *paths = [NSMutableArray arrayWithCapacity:[items count]];
|
2007-10-18 02:52:03 +00:00
|
|
|
|
2008-02-20 00:54:45 +00:00
|
|
|
for (id p in items) {
|
2008-02-20 01:01:15 +00:00
|
|
|
[urls addObject:[p URL]];
|
2009-08-16 16:49:34 +00:00
|
|
|
[paths addObject:[[p URL] path]];
|
2007-10-18 02:52:03 +00:00
|
|
|
}
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Paths: %@", paths);
|
2009-08-16 16:49:34 +00:00
|
|
|
[pboard declareTypes:[NSArray arrayWithObjects:CogUrlsPboardType,nil] owner:nil]; //add it to pboard
|
2008-02-13 17:14:19 +00:00
|
|
|
[pboard setData:[NSArchiver archivedDataWithRootObject:urls] forType:CogUrlsPboardType];
|
2009-08-16 16:49:34 +00:00
|
|
|
[pboard addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
|
|
|
|
[pboard setPropertyList:paths forType:NSFilenamesPboardType];
|
2007-10-18 02:52:03 +00:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reloadPathNode:(PathNode *)item
|
|
|
|
{
|
|
|
|
if (item == rootNode)
|
|
|
|
{
|
|
|
|
[outlineView reloadData];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[outlineView reloadItem:item reloadChildren:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|