2008-02-11 07:45:34 +00:00
|
|
|
//
|
|
|
|
// SpotlightWindowController.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Matthew Grinshpun on 10/02/08.
|
2008-02-14 14:07:10 +00:00
|
|
|
// Copyright 2008 Matthew Leon Grinshpun. All rights reserved.
|
2008-02-11 07:45:34 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "SpotlightWindowController.h"
|
2008-02-13 23:51:36 +00:00
|
|
|
#import "PlaylistLoader.h"
|
|
|
|
#import "SpotlightPlaylistEntry.h"
|
2008-02-14 14:07:10 +00:00
|
|
|
#import "NSComparisonPredicate+CogPredicate.h"
|
2008-02-16 13:05:30 +00:00
|
|
|
#import "NSArray+CogSort.h"
|
|
|
|
#import "NSString+CogSort.h"
|
2008-02-16 13:18:14 +00:00
|
|
|
#import "NSNumber+CogSort.h"
|
2008-02-16 22:59:27 +00:00
|
|
|
#import "SpotlightTransformers.h"
|
2008-02-11 07:45:34 +00:00
|
|
|
|
2008-02-13 23:51:36 +00:00
|
|
|
// Minimum length of a search string (searching for very small strings gets ugly)
|
|
|
|
#define MINIMUM_SEARCH_STRING_LENGTH 3
|
|
|
|
|
|
|
|
// Store a class predicate for searching for music
|
|
|
|
static NSPredicate * musicOnlyPredicate = nil;
|
2008-02-11 07:45:34 +00:00
|
|
|
|
|
|
|
@implementation SpotlightWindowController
|
|
|
|
|
2008-02-13 23:51:36 +00:00
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
musicOnlyPredicate = [[NSPredicate predicateWithFormat:
|
|
|
|
@"kMDItemContentTypeTree==\'public.audio\'"] retain];
|
2008-02-16 22:59:27 +00:00
|
|
|
|
|
|
|
// Register value transformers
|
2008-02-18 12:59:20 +00:00
|
|
|
NSValueTransformer *pausingQueryTransformer = [[[PausingQueryTransformer alloc]init]autorelease];
|
2008-02-16 22:59:27 +00:00
|
|
|
[NSValueTransformer setValueTransformer:pausingQueryTransformer forName:@"PausingQueryTransformer"];
|
2008-02-18 20:09:02 +00:00
|
|
|
|
2008-02-18 12:59:20 +00:00
|
|
|
NSValueTransformer *authorToArtistTransformer = [[[AuthorToArtistTransformer alloc]init]autorelease];
|
|
|
|
[NSValueTransformer setValueTransformer:authorToArtistTransformer forName:@"AuthorToArtistTransformer"];
|
2008-02-18 20:09:02 +00:00
|
|
|
|
2008-02-18 16:55:31 +00:00
|
|
|
NSValueTransformer *pathToURLTransformer = [[[PathToURLTransformer alloc]init]autorelease];
|
|
|
|
[NSValueTransformer setValueTransformer:pathToURLTransformer forName:@"PathToURLTransformers"];
|
2008-02-18 20:09:02 +00:00
|
|
|
|
|
|
|
NSValueTransformer *stringToSearchScopeTransformer = [[[StringToSearchScopeTransformer alloc]init]autorelease];
|
|
|
|
[NSValueTransformer setValueTransformer:stringToSearchScopeTransformer forName:@"StringToSearchScopeTransformer"];
|
2008-02-13 23:51:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-23 14:09:34 +00:00
|
|
|
- (void)registerDefaults
|
|
|
|
{
|
|
|
|
// Set the home directory as the default search directory
|
|
|
|
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
NSString * homeDir = @"~";
|
|
|
|
homeDir = [homeDir stringByExpandingTildeInPath];
|
|
|
|
homeDir = [[NSURL fileURLWithPath:homeDir isDirectory:YES] absoluteString];
|
|
|
|
NSDictionary *searchDefault =
|
|
|
|
[NSDictionary dictionaryWithObject:homeDir
|
|
|
|
forKey:@"spotlightSearchPath"];
|
|
|
|
[defaults registerDefaults:searchDefault];
|
|
|
|
}
|
|
|
|
|
2008-02-13 23:51:36 +00:00
|
|
|
- (id)init
|
|
|
|
{
|
2008-02-15 22:47:27 +00:00
|
|
|
if (self = [super initWithWindowNibName:@"SpotlightPanel"]) {
|
2008-02-18 20:09:02 +00:00
|
|
|
self.query = [[[NSMetadataQuery alloc]init]autorelease];
|
2008-02-13 23:51:36 +00:00
|
|
|
[self.query setDelegate:self];
|
2008-02-16 13:05:30 +00:00
|
|
|
self.query.sortDescriptors = [NSArray arrayWithObjects:
|
2009-09-02 04:14:07 +00:00
|
|
|
[[[NSSortDescriptor alloc]initWithKey:@"kMDItemAuthors"
|
|
|
|
ascending:YES
|
|
|
|
selector:@selector(compareFirstString:)] autorelease],
|
|
|
|
[[[NSSortDescriptor alloc]initWithKey:@"kMDItemAlbum"
|
|
|
|
ascending:YES
|
|
|
|
selector:@selector(caseInsensitiveCompare:)] autorelease],
|
|
|
|
[[[NSSortDescriptor alloc]initWithKey:@"kMDItemAudioTrackNumber"
|
|
|
|
ascending:YES
|
|
|
|
selector:@selector(compareTrackNumbers:)] autorelease],
|
|
|
|
nil];
|
2008-02-17 09:56:44 +00:00
|
|
|
|
|
|
|
// hook my query transformer up to me
|
|
|
|
[PausingQueryTransformer setSearchController:self];
|
2008-03-13 02:07:49 +00:00
|
|
|
|
|
|
|
[self registerDefaults];
|
|
|
|
|
|
|
|
// 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];
|
2008-02-13 23:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2008-02-18 20:46:53 +00:00
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-21 10:45:09 +00:00
|
|
|
- (IBAction)toggleWindow:(id)sender
|
|
|
|
{
|
|
|
|
if ([[self window] isVisible])
|
|
|
|
[[self window] orderOut:self];
|
|
|
|
else
|
|
|
|
[self showWindow:self];
|
|
|
|
}
|
|
|
|
|
2008-02-13 23:51:36 +00:00
|
|
|
- (void)performSearch
|
|
|
|
{
|
|
|
|
NSPredicate *searchPredicate;
|
|
|
|
// Process the search string into a compound predicate. If Nil is returned do nothing
|
|
|
|
if(searchPredicate = [self processSearchString])
|
|
|
|
{
|
|
|
|
// spotlightPredicate, which is what will finally be used for the spotlight search
|
|
|
|
// is the union of the (potentially) compound searchPredicate and the static
|
|
|
|
// musicOnlyPredicate
|
|
|
|
|
|
|
|
NSPredicate *spotlightPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
|
|
|
|
[NSArray arrayWithObjects: musicOnlyPredicate,
|
|
|
|
searchPredicate,
|
|
|
|
nil]];
|
|
|
|
// Only preform a new search if the predicate has changed or there is a new path
|
|
|
|
if(![self.query.predicate isEqual:spotlightPredicate]
|
|
|
|
|| ![self.query.searchScopes isEqualToArray:
|
|
|
|
[NSArray arrayWithObjects:pathControl.URL, nil]])
|
|
|
|
{
|
|
|
|
if([self.query isStarted])
|
|
|
|
[self.query stopQuery];
|
|
|
|
self.query.predicate = spotlightPredicate;
|
|
|
|
// Set scope to contents of pathControl
|
|
|
|
self.query.searchScopes = [NSArray arrayWithObjects:pathControl.URL, nil];
|
|
|
|
[self.query startQuery];
|
2008-02-16 13:18:14 +00:00
|
|
|
NSLog(@"Started query: %@", [self.query.predicate description]);
|
2008-02-13 23:51:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPredicate *)processSearchString
|
|
|
|
{
|
2008-02-14 14:07:10 +00:00
|
|
|
NSMutableArray *subpredicates = [NSMutableArray arrayWithCapacity:10];
|
2008-02-13 23:51:36 +00:00
|
|
|
|
|
|
|
NSScanner *scanner = [NSScanner scannerWithString:self.searchString];
|
2008-02-14 17:48:32 +00:00
|
|
|
BOOL exactString;
|
|
|
|
NSString * scannedString;
|
|
|
|
NSMutableString * parsingString;
|
2008-02-13 23:51:36 +00:00
|
|
|
while (![scanner isAtEnd])
|
|
|
|
{
|
2008-02-14 17:48:32 +00:00
|
|
|
exactString = NO;
|
2008-02-13 23:51:36 +00:00
|
|
|
if ([scanner scanUpToString:@" " intoString:&scannedString])
|
|
|
|
{
|
2008-02-14 14:07:10 +00:00
|
|
|
if ([scannedString length] < MINIMUM_SEARCH_STRING_LENGTH)
|
|
|
|
continue;
|
|
|
|
|
2008-02-14 17:48:32 +00:00
|
|
|
// We use NSMutableString because this string will get abused a bit
|
|
|
|
// It potentially could be reading the entire search string
|
|
|
|
|
|
|
|
parsingString = [NSMutableString stringWithCapacity: [self.searchString length]];
|
|
|
|
[parsingString setString: scannedString];
|
|
|
|
|
|
|
|
|
|
|
|
if ([parsingString characterAtIndex:0] == '%')
|
2008-02-13 23:51:36 +00:00
|
|
|
{
|
2008-02-14 17:48:32 +00:00
|
|
|
if ([parsingString length] < (MINIMUM_SEARCH_STRING_LENGTH + 2))
|
2008-02-14 14:07:10 +00:00
|
|
|
continue;
|
2008-02-14 17:48:32 +00:00
|
|
|
|
|
|
|
if ([parsingString characterAtIndex:2] == '\"')
|
|
|
|
{
|
|
|
|
exactString = YES;
|
|
|
|
// If the string does not end in a quotation mark and we're not at the end,
|
|
|
|
// scan until we find one.
|
|
|
|
// Allows strings within quotation marks to include spaces
|
|
|
|
if ([parsingString characterAtIndex:([parsingString length] - 1)] != '\"' &&
|
|
|
|
![scanner isAtEnd])
|
|
|
|
{
|
|
|
|
NSString *restOfString;
|
|
|
|
[scanner scanUpToString:@"\"" intoString:&restOfString];
|
|
|
|
[parsingString appendFormat:@" %@", restOfString];
|
|
|
|
}
|
|
|
|
else if ([parsingString characterAtIndex:([parsingString length] - 1)] == '\"')
|
|
|
|
{
|
|
|
|
// pick off the quotation mark at the end
|
|
|
|
[parsingString deleteCharactersInRange:
|
|
|
|
NSMakeRange([parsingString length] - 1, 1)];
|
|
|
|
|
|
|
|
}
|
|
|
|
// eliminate beginning quotation mark
|
|
|
|
[parsingString deleteCharactersInRange: NSMakeRange(2, 1)];
|
|
|
|
}
|
2008-02-14 14:07:10 +00:00
|
|
|
|
|
|
|
// Search for artist
|
2008-02-14 17:48:32 +00:00
|
|
|
if([parsingString characterAtIndex:1] == 'a')
|
2008-02-14 14:07:10 +00:00
|
|
|
{
|
|
|
|
[subpredicates addObject:
|
|
|
|
[NSComparisonPredicate predicateForMdKey:@"kMDItemAuthors"
|
2008-02-14 17:48:32 +00:00
|
|
|
withString:[parsingString substringFromIndex:2]
|
|
|
|
exactString:exactString]];
|
2008-02-14 14:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search for album
|
2008-02-14 17:48:32 +00:00
|
|
|
if([parsingString characterAtIndex:1] == 'l')
|
2008-02-14 14:07:10 +00:00
|
|
|
{
|
|
|
|
[subpredicates addObject:
|
|
|
|
[NSComparisonPredicate predicateForMdKey:@"kMDItemAlbum"
|
2008-02-14 17:48:32 +00:00
|
|
|
withString:[parsingString substringFromIndex:2]
|
|
|
|
exactString:exactString]];
|
2008-02-14 14:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search for title
|
2008-02-14 17:48:32 +00:00
|
|
|
if([parsingString characterAtIndex:1] == 't')
|
2008-02-14 14:07:10 +00:00
|
|
|
{
|
|
|
|
[subpredicates addObject:
|
|
|
|
[NSComparisonPredicate predicateForMdKey:@"kMDItemTitle"
|
2008-02-14 17:48:32 +00:00
|
|
|
withString:[parsingString substringFromIndex:2]
|
|
|
|
exactString:exactString]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for genre
|
|
|
|
if([parsingString characterAtIndex:1] == 'g')
|
|
|
|
{
|
|
|
|
[subpredicates addObject:
|
|
|
|
[NSComparisonPredicate predicateForMdKey:@"kMDItemMusicalGenre"
|
|
|
|
withString:[parsingString substringFromIndex:2]
|
|
|
|
exactString:exactString]];
|
2008-02-14 14:07:10 +00:00
|
|
|
}
|
2008-02-15 14:23:24 +00:00
|
|
|
|
|
|
|
// Search for comment
|
|
|
|
if([parsingString characterAtIndex:1] == 'c')
|
|
|
|
{
|
|
|
|
[subpredicates addObject:
|
|
|
|
[NSComparisonPredicate predicateForMdKey:@"kMDItemComment"
|
|
|
|
withString:[parsingString substringFromIndex:2]
|
|
|
|
exactString:exactString]];
|
|
|
|
}
|
2008-02-14 14:07:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-16 14:04:59 +00:00
|
|
|
NSString * wildcardString = [NSString stringWithFormat:@"*%@*", parsingString];
|
2008-03-08 23:47:29 +00:00
|
|
|
NSPredicate * pred = [NSPredicate predicateWithFormat:@"(kMDItemTitle LIKE[cd] %@) OR (kMDItemAlbum LIKE[cd] %@) OR (kMDItemAuthors LIKE[cd] %@)",
|
2008-02-16 14:04:59 +00:00
|
|
|
wildcardString, wildcardString, wildcardString];
|
|
|
|
[subpredicates addObject: pred];
|
2008-02-13 23:51:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([subpredicates count] == 0)
|
2008-02-16 22:59:27 +00:00
|
|
|
return nil;
|
2008-02-13 23:51:36 +00:00
|
|
|
else if ([subpredicates count] == 1)
|
|
|
|
return [subpredicates objectAtIndex: 0];
|
|
|
|
|
|
|
|
// Create a compound predicate from subPredicates
|
|
|
|
return [NSCompoundPredicate andPredicateWithSubpredicates: subpredicates];
|
|
|
|
}
|
|
|
|
|
2008-02-16 20:08:45 +00:00
|
|
|
- (void)searchForArtist:(NSString *)artist
|
|
|
|
{
|
|
|
|
[self showWindow:self];
|
|
|
|
self.searchString = [NSString stringWithFormat:@"%%a\"%@\"", artist];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)searchForAlbum:(NSString *)album
|
|
|
|
{
|
|
|
|
[self showWindow:self];
|
|
|
|
self.searchString = [NSString stringWithFormat:@"%%l\"%@\"", album];
|
|
|
|
}
|
|
|
|
|
2008-02-13 23:51:36 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
2008-02-18 20:09:02 +00:00
|
|
|
self.query = nil;
|
|
|
|
self.searchString = nil;
|
2008-02-13 23:51:36 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)addToPlaylist:(id)sender
|
|
|
|
{
|
2008-02-15 14:23:24 +00:00
|
|
|
NSArray *tracks;
|
2008-02-13 23:51:36 +00:00
|
|
|
[self.query disableUpdates];
|
2008-02-15 14:23:24 +00:00
|
|
|
tracks = playlistController.selectedObjects;
|
|
|
|
if ([tracks count] == 0)
|
|
|
|
tracks = playlistController.arrangedObjects;
|
2008-05-09 21:24:49 +00:00
|
|
|
|
2009-02-28 22:22:33 +00:00
|
|
|
[playlistLoader willInsertURLs:[tracks valueForKey:@"URL"] origin:URLOriginExternal];
|
|
|
|
[playlistLoader didInsertURLs:[playlistLoader addURLs:[tracks valueForKey:@"URL"] sort:NO] origin:URLOriginExternal];
|
2008-05-09 21:24:49 +00:00
|
|
|
[self.query enableUpdates];
|
2008-02-13 23:51:36 +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
|
|
|
|
|
|
|
|
@synthesize query;
|
|
|
|
|
|
|
|
@synthesize searchString;
|
|
|
|
- (void)setSearchString:(NSString *)aString
|
|
|
|
{
|
|
|
|
// Make sure the string is changed
|
|
|
|
if (![searchString isEqualToString:aString])
|
|
|
|
{
|
|
|
|
searchString = [aString copy];
|
|
|
|
[self performSearch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-11 07:45:34 +00:00
|
|
|
@end
|