2008-02-10 20:34:29 +00:00
|
|
|
//
|
|
|
|
// SpotlightSearchController.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Matthew Grinshpun on 10/02/08.
|
|
|
|
// Copyright 2008 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "SpotlightSearchController.h"
|
2008-02-11 09:20:52 +00:00
|
|
|
#import "SpotlightWindowController.h"
|
2008-02-10 20:34:29 +00:00
|
|
|
#import "PlaylistLoader.h"
|
2008-02-11 14:10:25 +00:00
|
|
|
#import "SpotlightPlaylistEntry.h"
|
2008-02-10 20:34:29 +00:00
|
|
|
|
|
|
|
// Store a class predicate for searching for music
|
|
|
|
static NSPredicate * musicOnlyPredicate = nil;
|
|
|
|
|
|
|
|
@implementation SpotlightSearchController
|
|
|
|
|
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
musicOnlyPredicate = [[NSPredicate predicateWithFormat:
|
|
|
|
@"kMDItemContentTypeTree==\'public.audio\'"] retain];
|
2008-02-11 18:51:04 +00:00
|
|
|
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
2008-02-12 10:30:32 +00:00
|
|
|
|
|
|
|
// Set the home directory as the default search directory
|
2008-02-11 18:51:04 +00:00
|
|
|
NSString * homeDir = @"~";
|
|
|
|
homeDir = [homeDir stringByExpandingTildeInPath];
|
|
|
|
homeDir = [[NSURL fileURLWithPath:homeDir isDirectory:YES] absoluteString];
|
|
|
|
NSDictionary *searchDefault =
|
|
|
|
[NSDictionary dictionaryWithObject:homeDir
|
|
|
|
forKey:@"spotlightSearchPath"];
|
|
|
|
[defaults registerDefaults:searchDefault];
|
2008-02-10 20:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
self.query = [[NSMetadataQuery alloc] init];
|
2008-02-11 14:10:25 +00:00
|
|
|
[self.query setDelegate:self];
|
2008-02-10 20:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)performSearch
|
|
|
|
{
|
2008-02-12 16:24:09 +00:00
|
|
|
// Process the search string into a compound predicate
|
|
|
|
NSPredicate *searchPredicate = [self processSearchString];
|
|
|
|
|
2008-02-12 10:30:32 +00:00
|
|
|
// Set scope to contents of pathControl
|
2008-02-11 10:29:21 +00:00
|
|
|
[self.query setSearchScopes:[NSArray arrayWithObjects:pathControl.URL, nil]];
|
2008-02-12 16:24:09 +00:00
|
|
|
|
2008-02-10 20:34:29 +00:00
|
|
|
// spotlightPredicate, which is what will finally be used for the spotlight search
|
2008-02-12 16:24:09 +00:00
|
|
|
// is the union of the (potentially) compound searchPredicate and the static
|
|
|
|
// musicOnlyPredicate
|
2008-02-10 20:34:29 +00:00
|
|
|
|
|
|
|
NSPredicate *spotlightPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
|
|
|
|
[NSArray arrayWithObjects: musicOnlyPredicate,
|
|
|
|
searchPredicate,
|
|
|
|
nil]];
|
|
|
|
if([self.query isStarted])
|
|
|
|
[self.query stopQuery];
|
|
|
|
self.query.predicate = spotlightPredicate;
|
|
|
|
[self.query startQuery];
|
|
|
|
NSLog(@"Started query: %@", [self.query.predicate description], [[self.query class]description]);
|
|
|
|
}
|
|
|
|
|
2008-02-12 16:24:09 +00:00
|
|
|
- (NSPredicate *)processSearchString
|
|
|
|
{
|
2008-02-13 16:10:59 +00:00
|
|
|
// a somewhat dumb way to collapse the whitespace in searchString
|
|
|
|
// TODO: write a more elegant way of accomplishing this
|
|
|
|
NSString * compareString = [self.searchString
|
|
|
|
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
|
|
|
|
|
|
NSString * collapsedString = [compareString stringByReplacingOccurrencesOfString:@" "
|
|
|
|
withString:@" "];
|
|
|
|
while (![collapsedString isEqualToString:compareString])
|
|
|
|
{
|
|
|
|
compareString = [collapsedString copy];
|
|
|
|
collapsedString = [compareString stringByReplacingOccurrencesOfString:@" "
|
|
|
|
withString:@" "];
|
|
|
|
}
|
|
|
|
|
2008-02-12 16:24:09 +00:00
|
|
|
// break the string up into an array of each word
|
2008-02-13 16:10:59 +00:00
|
|
|
NSArray * searchComponents = [collapsedString
|
2008-02-12 16:24:09 +00:00
|
|
|
componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
|
|
|
|
|
|
// create an array of all the predicates to join together
|
|
|
|
NSMutableArray * subpredicates = [NSMutableArray
|
|
|
|
arrayWithCapacity:[searchComponents count]];
|
|
|
|
|
|
|
|
// we will ignore case and diacritics
|
|
|
|
unsigned options = (NSCaseInsensitivePredicateOption|
|
|
|
|
NSDiacriticInsensitivePredicateOption);
|
|
|
|
|
|
|
|
for(NSString *s in searchComponents)
|
|
|
|
{
|
|
|
|
// convert each "word" into "*word*"
|
|
|
|
NSString *processedKey = [NSString stringWithFormat: @"*%@*", s];
|
|
|
|
|
|
|
|
// Search all tags for something like word
|
|
|
|
NSPredicate *predicate = [NSComparisonPredicate
|
|
|
|
predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"*"]
|
|
|
|
rightExpression:[NSExpression expressionForConstantValue:processedKey]
|
|
|
|
modifier:NSDirectPredicateModifier
|
|
|
|
type:NSLikePredicateOperatorType
|
|
|
|
options:options];
|
|
|
|
|
|
|
|
//TODO: Ability to search only artist, albums, etc.
|
|
|
|
[subpredicates addObject: predicate];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([subpredicates count] == 1)
|
|
|
|
return [subpredicates objectAtIndex: 0];
|
|
|
|
|
|
|
|
// Create a compound predicate from subPredicates
|
|
|
|
return [NSCompoundPredicate andPredicateWithSubpredicates: subpredicates];
|
|
|
|
}
|
|
|
|
|
2008-02-10 20:34:29 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self.query stopQuery];
|
|
|
|
[self.query release];
|
|
|
|
[self.searchString release];
|
|
|
|
[musicOnlyPredicate release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2008-02-11 10:29:21 +00:00
|
|
|
- (IBAction)changeSearchPath:(id)sender
|
|
|
|
{
|
|
|
|
// When the search path is changed, restart search
|
|
|
|
if([self.query isStarted]) {
|
|
|
|
[self performSearch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)addToPlaylist:(id)sender
|
2008-02-10 20:34:29 +00:00
|
|
|
{
|
2008-02-11 09:20:52 +00:00
|
|
|
[self.query disableUpdates];
|
|
|
|
|
2008-02-13 16:10:59 +00:00
|
|
|
NSArray *urls = [[playlistController selectedObjects]valueForKey:@"url"];
|
|
|
|
[spotlightWindowController.playlistLoader addURLs:urls sort:NO];
|
2008-02-11 09:20:52 +00:00
|
|
|
|
|
|
|
[self.query enableUpdates];
|
2008-02-10 20:34:29 +00:00
|
|
|
}
|
|
|
|
|
2008-02-11 14:10:25 +00:00
|
|
|
#pragma mark NSMetadataQuery delegate methods
|
|
|
|
|
|
|
|
// replace the NSMetadataItem with a PlaylistEntry
|
|
|
|
- (id)metadataQuery:(NSMetadataQuery*)query
|
|
|
|
replacementObjectForResultObject:(NSMetadataItem*)result
|
|
|
|
{
|
|
|
|
return [SpotlightPlaylistEntry playlistEntryWithMetadataItem: result];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Getters and setters
|
|
|
|
|
2008-02-10 20:34:29 +00:00
|
|
|
@synthesize query;
|
|
|
|
|
|
|
|
@synthesize searchString;
|
|
|
|
- (void)setSearchString:(NSString *)aString
|
|
|
|
{
|
|
|
|
if (searchString != aString) {
|
|
|
|
searchString = [aString copy];
|
|
|
|
[self performSearch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|