Consolidated Spotlight code.

CQTexperiment
matthewleon 2008-02-13 23:51:36 +00:00
parent c25dfa6535
commit d795c59c10
8 changed files with 859 additions and 989 deletions

View File

@ -103,7 +103,6 @@
17F3BB890CBC565900864489 /* CueSheet.bundle in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17F3BB880CBC565100864489 /* CueSheet.bundle */; };
17F561400C3BD4F30019975C /* CogAudio.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17F561330C3BD4DC0019975C /* CogAudio.framework */; };
17F562390C3BD91B0019975C /* General.preferencePane in Resources */ = {isa = PBXBuildFile; fileRef = 17F5622E0C3BD8FB0019975C /* General.preferencePane */; };
5604D4550D603430004F5C5D /* SpotlightSearchController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5604D4510D603430004F5C5D /* SpotlightSearchController.m */; };
5604D4580D603459004F5C5D /* SpotlightPanel.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5604D4570D603459004F5C5D /* SpotlightPanel.xib */; };
5604D45B0D60349B004F5C5D /* SpotlightWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5604D4590D60349B004F5C5D /* SpotlightWindowController.m */; };
5604D4F60D60726E004F5C5D /* SpotlightPlaylistEntry.m in Sources */ = {isa = PBXBuildFile; fileRef = 5604D4F50D60726E004F5C5D /* SpotlightPlaylistEntry.m */; };
@ -592,8 +591,6 @@
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32CA4F630368D1EE00C91783 /* Cog_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cog_Prefix.pch; sourceTree = "<group>"; };
5604D4510D603430004F5C5D /* SpotlightSearchController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SpotlightSearchController.m; path = Spotlight/SpotlightSearchController.m; sourceTree = "<group>"; };
5604D4520D603430004F5C5D /* SpotlightSearchController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SpotlightSearchController.h; path = Spotlight/SpotlightSearchController.h; sourceTree = "<group>"; };
5604D4570D603459004F5C5D /* SpotlightPanel.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = SpotlightPanel.xib; path = Spotlight/SpotlightPanel.xib; sourceTree = "<group>"; };
5604D4590D60349B004F5C5D /* SpotlightWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SpotlightWindowController.m; path = Spotlight/SpotlightWindowController.m; sourceTree = "<group>"; };
5604D45A0D60349B004F5C5D /* SpotlightWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SpotlightWindowController.h; path = Spotlight/SpotlightWindowController.h; sourceTree = "<group>"; };
@ -1098,8 +1095,6 @@
56462EAE0D6341F6000AB68C /* SpotlightTransformers.m */,
5604D4590D60349B004F5C5D /* SpotlightWindowController.m */,
5604D45A0D60349B004F5C5D /* SpotlightWindowController.h */,
5604D4510D603430004F5C5D /* SpotlightSearchController.m */,
5604D4520D603430004F5C5D /* SpotlightSearchController.h */,
5604D4F40D60726E004F5C5D /* SpotlightPlaylistEntry.h */,
5604D4F50D60726E004F5C5D /* SpotlightPlaylistEntry.m */,
56462DD80D61D71E000AB68C /* SpotlightPlaylistView.h */,
@ -1638,7 +1633,6 @@
178BAB9B0CD4E1B700B33D47 /* PopupButton.m in Sources */,
17BBE5BC0CD95CFA00258F7A /* InvertedToolbarWindow.m in Sources */,
173428F50D5FB1C400E8D854 /* EntriesController.m in Sources */,
5604D4550D603430004F5C5D /* SpotlightSearchController.m in Sources */,
5604D45B0D60349B004F5C5D /* SpotlightWindowController.m in Sources */,
5604D4F60D60726E004F5C5D /* SpotlightPlaylistEntry.m in Sources */,
56462DDA0D61D71E000AB68C /* SpotlightPlaylistView.m in Sources */,

File diff suppressed because it is too large Load Diff

View File

@ -8,10 +8,10 @@
#import <Cocoa/Cocoa.h>
#import "PlaylistController.h"
#import "SpotlightSearchController.h"
#import "SpotlightWindowController.h"
@interface SpotlightPlaylistController : PlaylistController {
IBOutlet SpotlightSearchController * spotlightSearchController;
IBOutlet SpotlightWindowController * spotlightWindowController;
}
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard;

View File

@ -15,13 +15,13 @@
writeRowsWithIndexes:(NSIndexSet *)rowIndexes
toPasteboard:(NSPasteboard*)pboard
{
[spotlightSearchController.query disableUpdates];
[spotlightWindowController.query disableUpdates];
NSArray *urls = [[self selectedObjects]valueForKey:@"url"];
[pboard declareTypes:[NSArray arrayWithObjects:CogUrlsPboardType,nil] owner:nil]; //add it to pboard
[pboard setData:[NSArchiver archivedDataWithRootObject:urls] forType:CogUrlsPboardType];
[spotlightSearchController.query enableUpdates];
[spotlightWindowController.query enableUpdates];
return YES;
}

View File

@ -1,31 +0,0 @@
//
// SpotlightSearchController.h
// Cog
//
// Created by Matthew Grinshpun on 10/02/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@class SpotlightWindowController;
@interface SpotlightSearchController : NSObject {
IBOutlet NSArrayController *playlistController;
IBOutlet SpotlightWindowController *spotlightWindowController;
IBOutlet NSPathControl *pathControl;
NSMetadataQuery *query;
NSString *searchString;
NSString *spotlightSearchPath;
}
- (IBAction)addToPlaylist:(id)sender;
- (void)performSearch;
- (NSPredicate *)processSearchString;
@property(retain) NSMetadataQuery *query;
@property(copy) NSString *searchString;
@property(copy) NSString *spotlightSearchPath;
@end

View File

@ -1,198 +0,0 @@
//
// SpotlightSearchController.m
// Cog
//
// Created by Matthew Grinshpun on 10/02/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "SpotlightSearchController.h"
#import "SpotlightWindowController.h"
#import "PlaylistLoader.h"
#import "SpotlightPlaylistEntry.h"
// 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;
@implementation SpotlightSearchController
+ (void)initialize
{
musicOnlyPredicate = [[NSPredicate predicateWithFormat:
@"kMDItemContentTypeTree==\'public.audio\'"] retain];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Set the home directory as the default search directory
NSString * homeDir = @"~";
homeDir = [homeDir stringByExpandingTildeInPath];
homeDir = [[NSURL fileURLWithPath:homeDir isDirectory:YES] absoluteString];
NSDictionary *searchDefault =
[NSDictionary dictionaryWithObject:homeDir
forKey:@"spotlightSearchPath"];
[defaults registerDefaults:searchDefault];
}
- (id)init
{
if (self = [super init]) {
self.query = [[NSMetadataQuery alloc] init];
[self.query setDelegate:self];
}
return self;
}
- (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];
NSLog(@"Started query: %@", [self.query.predicate description]);
}
}
}
- (NSPredicate *)processSearchString
{
NSMutableArray *searchComponents = [NSMutableArray arrayWithCapacity:10];
NSScanner *scanner = [NSScanner scannerWithString:self.searchString];
while (![scanner isAtEnd])
{
NSString *scannedString;
if ([scanner scanUpToString:@" " intoString:&scannedString])
{
// don't add tiny strings in... They're make the system go nuts
// TODO: Maybe consider a better solution to the issue?
if ([scannedString length] >= MINIMUM_SEARCH_STRING_LENGTH)
{
[searchComponents addObject:scannedString];
}
}
}
// 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] == 0)
return Nil;
else if ([subpredicates count] == 1)
return [subpredicates objectAtIndex: 0];
// Create a compound predicate from subPredicates
return [NSCompoundPredicate andPredicateWithSubpredicates: subpredicates];
}
- (void)dealloc
{
[self.query stopQuery];
[self.query release];
[self.searchString release];
[musicOnlyPredicate release];
[super dealloc];
}
- (IBAction)addToPlaylist:(id)sender
{
[self.query disableUpdates];
NSArray *urls = [[playlistController selectedObjects]valueForKey:@"url"];
[spotlightWindowController.playlistLoader addURLs:urls sort:NO];
[self.query enableUpdates];
}
#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];
}
}
@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 around 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

View File

@ -12,8 +12,20 @@
@interface SpotlightWindowController : NSWindowController {
IBOutlet PlaylistLoader *playlistLoader;
IBOutlet NSArrayController *playlistController;
IBOutlet NSPathControl *pathControl;
NSMetadataQuery *query;
NSString *searchString;
NSString *spotlightSearchPath;
}
@property(retain) PlaylistLoader *playlistLoader;
- (IBAction)addToPlaylist:(id)sender;
- (void)performSearch;
- (NSPredicate *)processSearchString;
@property(retain) NSMetadataQuery *query;
@property(copy) NSString *searchString;
@property(copy) NSString *spotlightSearchPath;
@end

View File

@ -7,10 +7,191 @@
//
#import "SpotlightWindowController.h"
#import "PlaylistLoader.h"
#import "SpotlightPlaylistEntry.h"
// 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;
@implementation SpotlightWindowController
@synthesize playlistLoader;
+ (void)initialize
{
musicOnlyPredicate = [[NSPredicate predicateWithFormat:
@"kMDItemContentTypeTree==\'public.audio\'"] retain];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Set the home directory as the default search directory
NSString * homeDir = @"~";
homeDir = [homeDir stringByExpandingTildeInPath];
homeDir = [[NSURL fileURLWithPath:homeDir isDirectory:YES] absoluteString];
NSDictionary *searchDefault =
[NSDictionary dictionaryWithObject:homeDir
forKey:@"spotlightSearchPath"];
[defaults registerDefaults:searchDefault];
}
- (id)init
{
if (self = [super init]) {
self.query = [[NSMetadataQuery alloc] init];
[self.query setDelegate:self];
}
return self;
}
- (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];
NSLog(@"Started query: %@", [self.query.predicate description]);
}
}
}
- (NSPredicate *)processSearchString
{
NSMutableArray *searchComponents = [NSMutableArray arrayWithCapacity:10];
NSScanner *scanner = [NSScanner scannerWithString:self.searchString];
while (![scanner isAtEnd])
{
NSString *scannedString;
if ([scanner scanUpToString:@" " intoString:&scannedString])
{
// don't add tiny strings in... They're make the system go nuts
// TODO: Maybe consider a better solution to the issue?
if ([scannedString length] >= MINIMUM_SEARCH_STRING_LENGTH)
{
[searchComponents addObject:scannedString];
}
}
}
// 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] == 0)
return Nil;
else if ([subpredicates count] == 1)
return [subpredicates objectAtIndex: 0];
// Create a compound predicate from subPredicates
return [NSCompoundPredicate andPredicateWithSubpredicates: subpredicates];
}
- (void)dealloc
{
[self.query stopQuery];
[self.query release];
[self.searchString release];
[musicOnlyPredicate release];
[super dealloc];
}
- (IBAction)addToPlaylist:(id)sender
{
[self.query disableUpdates];
NSArray *urls = [[playlistController selectedObjects]valueForKey:@"url"];
[playlistLoader addURLs:urls sort:NO];
[self.query enableUpdates];
}
#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];
}
}
@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 around 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