From e8242824a89a970ce85de233309c65a429a9e884 Mon Sep 17 00:00:00 2001 From: vspader Date: Tue, 26 Sep 2006 22:22:56 +0000 Subject: [PATCH] Basic smartfolder support for file drawer. --- Cog.xcodeproj/project.pbxproj | 8 ++ FileDrawer/DirectoryNode.m | 53 +++++++---- FileDrawer/FileTreeController.m | 3 + FileDrawer/PathNode.m | 1 - FileDrawer/SmartFolderNode.h | 18 ++++ FileDrawer/SmartFolderNode.m | 160 ++++++++++++++++++++++++++++++++ 6 files changed, 223 insertions(+), 20 deletions(-) create mode 100644 FileDrawer/SmartFolderNode.h create mode 100644 FileDrawer/SmartFolderNode.m diff --git a/Cog.xcodeproj/project.pbxproj b/Cog.xcodeproj/project.pbxproj index ce3f69760..d91e7912d 100644 --- a/Cog.xcodeproj/project.pbxproj +++ b/Cog.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 171678BF0AC8C39E00C28CF3 /* SmartFolderNode.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 171678BD0AC8C39E00C28CF3 /* SmartFolderNode.h */; }; + 171678C00AC8C39E00C28CF3 /* SmartFolderNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 171678BE0AC8C39E00C28CF3 /* SmartFolderNode.m */; }; 8D11072A0486CEB800E47090 /* MainMenu.nib in Resources */ = {isa = PBXBuildFile; fileRef = 29B97318FDCFA39411CA2CEA /* MainMenu.nib */; }; 8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; }; 8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; }; @@ -183,6 +185,7 @@ 8E07AAF20AAC910500A4B32F /* SS_PrefsController.h in CopyFiles */, 8E07AB780AAC930B00A4B32F /* PreferencesController.h in CopyFiles */, 8E7C2B160AACE0F2009B4EAD /* NDHotKeyEvent.h in CopyFiles */, + 171678BF0AC8C39E00C28CF3 /* SmartFolderNode.h in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -192,6 +195,8 @@ 089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = ""; }; 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = ""; }; 13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = ""; }; + 171678BD0AC8C39E00C28CF3 /* SmartFolderNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SmartFolderNode.h; sourceTree = ""; }; + 171678BE0AC8C39E00C28CF3 /* SmartFolderNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SmartFolderNode.m; sourceTree = ""; }; 29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 29B97319FDCFA39411CA2CEA /* English */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = English; path = English.lproj/MainMenu.nib; sourceTree = ""; }; 29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = ""; }; @@ -714,6 +719,8 @@ 8EFFCD470AA093AF00C458A5 /* FileNode.m */, 8EFFCD520AA093AF00C458A5 /* PathNode.h */, 8EFFCD530AA093AF00C458A5 /* PathNode.m */, + 171678BD0AC8C39E00C28CF3 /* SmartFolderNode.h */, + 171678BE0AC8C39E00C28CF3 /* SmartFolderNode.m */, ); path = FileDrawer; sourceTree = ""; @@ -878,6 +885,7 @@ 8E07AAF30AAC910500A4B32F /* SS_PrefsController.m in Sources */, 8E07AB790AAC930B00A4B32F /* PreferencesController.m in Sources */, 8E7C2B170AACE0F2009B4EAD /* NDHotKeyEvent.m in Sources */, + 171678C00AC8C39E00C28CF3 /* SmartFolderNode.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/FileDrawer/DirectoryNode.m b/FileDrawer/DirectoryNode.m index 23efcf575..257970408 100644 --- a/FileDrawer/DirectoryNode.m +++ b/FileDrawer/DirectoryNode.m @@ -8,6 +8,7 @@ #import "DirectoryNode.h" #import "FileNode.h" +#import "SmartFolderNode.h" @implementation DirectoryNode @@ -39,24 +40,27 @@ return NO; } -- (NSArray *)subpaths +- (void)processContents: (NSArray *)contents { - if (subpaths == nil) + NSEnumerator *e = [contents objectEnumerator]; + NSString *s; + while ((s = [e nextObject])) { - subpaths = [[NSMutableArray alloc] init]; - NSArray *contents = [[NSFileManager defaultManager] directoryContentsAtPath:path]; - NSEnumerator *e = [contents objectEnumerator]; - NSString *s; - while ((s = [e nextObject])) + if ([s characterAtIndex:0] == '.') + { + continue; + } + + PathNode *newNode; + NSString *newSubpath = [path stringByAppendingPathComponent: s]; + + if ([[s pathExtension] caseInsensitiveCompare:@"savedSearch"] == NSOrderedSame) + { + newNode = [[SmartFolderNode alloc] initWithPath:newSubpath controller:controller]; + } + else { - if ([s characterAtIndex:0] == '.') - { - continue; - } - BOOL isDir; - PathNode *newNode; - NSString *newSubpath = [path stringByAppendingPathComponent: s]; [[NSFileManager defaultManager] fileExistsAtPath:newSubpath isDirectory:&isDir]; @@ -69,15 +73,26 @@ newNode = [[DirectoryNode alloc] initWithPath: newSubpath controller:controller]; else newNode = [[FileNode alloc] initWithPath: newSubpath]; - - [subpaths addObject:newNode]; - - [newNode release]; } + + [subpaths addObject:newNode]; + + [newNode release]; + } +} + +- (NSArray *)subpaths +{ + if (subpaths == nil) + { + subpaths = [[NSMutableArray alloc] init]; + NSArray *contents = [[NSFileManager defaultManager] directoryContentsAtPath:path]; + + [self processContents: contents]; + [[controller watcher] addPath:[self path]]; } -// NSLog(@"subpaths; %@", subpaths); return subpaths; } diff --git a/FileDrawer/FileTreeController.m b/FileDrawer/FileTreeController.m index 0ba67c799..ef34f7ae4 100644 --- a/FileDrawer/FileTreeController.m +++ b/FileDrawer/FileTreeController.m @@ -40,6 +40,9 @@ [r retain]; [rootPath release]; rootPath = r; + + [watcher removePath:r]; + [watcher addPath:rootPath]; [self refreshRoot]; } diff --git a/FileDrawer/PathNode.m b/FileDrawer/PathNode.m index 9aaaf0750..9ac6fd626 100644 --- a/FileDrawer/PathNode.m +++ b/FileDrawer/PathNode.m @@ -48,5 +48,4 @@ pathIcon = pi; } - @end diff --git a/FileDrawer/SmartFolderNode.h b/FileDrawer/SmartFolderNode.h new file mode 100644 index 000000000..f5cab29eb --- /dev/null +++ b/FileDrawer/SmartFolderNode.h @@ -0,0 +1,18 @@ +// +// SmartFolderNode.h +// Cog +// +// Created by Vincent Spader on 9/25/06. +// Copyright 2006 __MyCompanyName__. All rights reserved. +// + +#import + +#import "PathNode.h" + +@interface SmartFolderNode : PathNode { + NSMutableArray *subpaths; + id controller; +} + +@end diff --git a/FileDrawer/SmartFolderNode.m b/FileDrawer/SmartFolderNode.m new file mode 100644 index 000000000..cb079a231 --- /dev/null +++ b/FileDrawer/SmartFolderNode.m @@ -0,0 +1,160 @@ +// +// SmartFolderNode.m +// Cog +// +// Created by Vincent Spader on 9/25/06. +// Copyright 2006 __MyCompanyName__. All rights reserved. +// + +#import "SmartFolderNode.h" +#import "DirectoryNode.h" +#import "FileNode.h" + +@implementation SmartFolderNode + +-(id)initWithPath:(NSString *)p controller:(id) c +{ + self = [super initWithPath:p]; + if (self) + { + controller = [c retain]; + } + + return self; +} + +- (void)dealloc { + if (subpaths) + [subpaths release]; + + [super dealloc]; +} + + +- (BOOL)isLeaf +{ + return NO; +} + +//need to merge this and directorynode +- (void)processContents: (NSArray *)contents +{ + NSLog(@"PROCESSING: %@", contents); + NSEnumerator *e = [contents objectEnumerator]; + NSString *s; + + while (s = [e nextObject]) + { + NSLog(@"STRING: %@", s); +/* if ([s characterAtIndex:0] == '.') + { + continue; + } +*/ + PathNode *newNode; +// NSString *newSubpath = [path stringByAppendingPathComponent: s]; + + if ([[s pathExtension] caseInsensitiveCompare:@"savedSearch"] == NSOrderedSame) + { + newNode = [[SmartFolderNode alloc] initWithPath:s controller:controller]; + } + else + { + BOOL isDir; + + [[NSFileManager defaultManager] fileExistsAtPath:s isDirectory:&isDir]; + + NSLog(@"IS IT ACCEPTABLE?"); + if (!isDir && ![[controller acceptableFileTypes] containsObject:[s pathExtension]]) + continue; + NSLog(@"IS IT A FILE?"); + if (isDir) + newNode = [[DirectoryNode alloc] initWithPath: s controller:controller]; + else + newNode = [[FileNode alloc] initWithPath: s]; + } + + [subpaths addObject:newNode]; + NSLog(@"SUBPATHS IN PROCESS: %@", subpaths); + [newNode release]; + } +} + +- (NSArray *)subpaths +{ + if (subpaths == nil) + { + subpaths = [[NSMutableArray alloc] init]; + + NSDictionary *doc = [NSDictionary dictionaryWithContentsOfFile:path]; + NSString *rawQuery = [doc objectForKey:@"RawQuery"]; + NSArray *searchPaths = [[doc objectForKey:@"SearchCriteria"] objectForKey:@"CurrentFolderPath"]; + + NSLog(@"Query: %@", rawQuery); + // Ugh, Carbon from now on... + MDQueryRef query = MDQueryCreate(kCFAllocatorDefault, (CFStringRef)rawQuery, NULL, NULL); + + MDQuerySetSearchScope(query, (CFArrayRef)searchPaths, 0); + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryFinished:) name:(NSString*)kMDQueryDidFinishNotification object:(id)query]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryUpdate:) name:(NSString*)kMDQueryDidUpdateNotification object:(id)query]; + + NSLog(@"PATHS: %@", searchPaths); + MDQueryExecute(query, kMDQueryWantsUpdates | kMDQuerySynchronous); + NSLog(@"QUERY FINISHED: %@", subpaths); + } + + return subpaths; +} + +- (void)setSubpaths:(id)s +{ + [s retain]; + [subpaths release]; + subpaths = s; +} + +- (unsigned int)countOfSubpaths +{ + return [[self subpaths] count]; +} + +- (PathNode *)objectInSubpathsAtIndex:(unsigned int)index +{ + return [[self subpaths] objectAtIndex:index]; +} + +- (void)queryFinished:(NSNotification *)notification +{ + MDQueryRef query = [notification object]; + + NSMutableArray *results = [NSMutableArray array]; + + int c = MDQueryGetResultCount(query); + + int i; + for (i = 0; i < c; i++) + { + MDItemRef item = (MDItemRef)MDQueryGetResultAtIndex(query, i); + + NSString *itemPath = (NSString*)MDItemCopyAttribute(item, kMDItemPath); + NSLog(@"RESULT PATH: %@", itemPath); + + [results addObject:itemPath]; + + [itemPath release]; + } + + [self processContents:results]; + NSLog(@"CONTENTS PROCESSED"); +} + +- (void)queryUpdate:(NSNotification *)notification +{ + MDQueryRef query = [notification object]; + NSLog(@"QUERY UPDATE: %@", notification); +} + + +@end +