Spotlight: Elegant solution to storing search scopes.

CQTexperiment
matthewleon 2008-02-18 20:09:02 +00:00
parent 08a4bbe08e
commit 67816346e0
5 changed files with 609 additions and 597 deletions

File diff suppressed because it is too large Load Diff

View File

@ -19,9 +19,10 @@
@end
@interface AuthorToArtistTransformer: NSValueTransformer {}
@end
@interface PathToURLTransformer: NSValueTransformer {}
@end
@interface StringToSearchScopeTransformer: NSValueTransformer {}
@end

View File

@ -69,4 +69,19 @@ static SpotlightWindowController * searchController;
return [value path];
}
@end
@implementation StringToSearchScopeTransformer
+ (Class)transformedValueClass { return [NSArray class]; }
+ (BOOL)allowsReverseTransformation { return NO; }
// Convert from URL string to Search Scope
- (id)transformedValue:(id)value {
if (value == nil) return nil;
NSURL *scope = [NSURL URLWithString:value];
return [NSArray arrayWithObject: scope];
}
@end

View File

@ -17,7 +17,6 @@
IBOutlet NSSearchField *searchField;
NSMetadataQuery *query;
NSString *searchString;
NSString *spotlightSearchPath;
}
- (IBAction)addToPlaylist:(id)sender;
@ -30,6 +29,5 @@
@property(retain) NSMetadataQuery *query;
@property(copy) NSString *searchString;
@property(copy) NSString *spotlightSearchPath;
@end

View File

@ -42,16 +42,22 @@ static NSPredicate * musicOnlyPredicate = nil;
// Register value transformers
NSValueTransformer *pausingQueryTransformer = [[[PausingQueryTransformer alloc]init]autorelease];
[NSValueTransformer setValueTransformer:pausingQueryTransformer forName:@"PausingQueryTransformer"];
NSValueTransformer *authorToArtistTransformer = [[[AuthorToArtistTransformer alloc]init]autorelease];
[NSValueTransformer setValueTransformer:authorToArtistTransformer forName:@"AuthorToArtistTransformer"];
NSValueTransformer *pathToURLTransformer = [[[PathToURLTransformer alloc]init]autorelease];
[NSValueTransformer setValueTransformer:pathToURLTransformer forName:@"PathToURLTransformers"];
NSValueTransformer *stringToSearchScopeTransformer = [[[StringToSearchScopeTransformer alloc]init]autorelease];
[NSValueTransformer setValueTransformer:stringToSearchScopeTransformer forName:@"StringToSearchScopeTransformer"];
}
- (id)init
{
if (self = [super initWithWindowNibName:@"SpotlightPanel"]) {
self.query = [[NSMetadataQuery alloc] init];
self.query = [[[NSMetadataQuery alloc]init]autorelease];
[self.query setDelegate:self];
self.query.sortDescriptors = [NSArray arrayWithObjects:
[[NSSortDescriptor alloc]initWithKey:@"kMDItemAuthors"
@ -65,6 +71,17 @@ static NSPredicate * musicOnlyPredicate = nil;
selector:@selector(compareTrackNumbers:)],
nil];
// We want to bind the query's search scope to the user default that is
// set from the NSPathControl.
NSDictionary *bindOptions =
[NSDictionary dictionaryWithObject:@"StringToSearchScopeTransformer"
forKey:NSValueTransformerNameBindingOption];
[self.query bind:@"searchScopes"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.spotlightSearchPath"
options:bindOptions];
// hook my query transformer up to me
[PausingQueryTransformer setSearchController:self];
}
@ -234,9 +251,8 @@ static NSPredicate * musicOnlyPredicate = nil;
- (void)dealloc
{
[self.query stopQuery];
[self.query release];
[self.searchString release];
self.query = nil;
self.searchString = nil;
[musicOnlyPredicate release];
[super dealloc];
}
@ -277,27 +293,4 @@ replacementObjectForResultObject:(NSMetadataItem*)result
}
}
@dynamic spotlightSearchPath;
// getter reads from user defaults
- (NSString *)spotlightSearchPath
{
return [[[NSUserDefaults standardUserDefaults]
stringForKey:@"spotlightSearchPath"]copy];
}
// Normally, our NSPathcontrol would just bind to the user defaults
// However, this does not allow us to perform a new search when
// the path changes. This getter/setter combo wraps the user
// defaults while performing a new search when the value changes.
- (void)setSpotlightSearchPath:(NSString *)aString
{
// Make sure the string is changed
if (![spotlightSearchPath isEqualToString: aString])
{
spotlightSearchPath = [aString copy];
[[NSUserDefaults standardUserDefaults] setObject:spotlightSearchPath
forKey:@"spotlightSearchPath"];
[self performSearch];
}
}
@end