Spotlight: Elegant solution to storing search scopes.
parent
08a4bbe08e
commit
67816346e0
File diff suppressed because it is too large
Load Diff
|
@ -19,9 +19,10 @@
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface AuthorToArtistTransformer: NSValueTransformer {}
|
@interface AuthorToArtistTransformer: NSValueTransformer {}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@interface PathToURLTransformer: NSValueTransformer {}
|
@interface PathToURLTransformer: NSValueTransformer {}
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface StringToSearchScopeTransformer: NSValueTransformer {}
|
||||||
@end
|
@end
|
|
@ -69,4 +69,19 @@ static SpotlightWindowController * searchController;
|
||||||
return [value path];
|
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
|
@end
|
|
@ -17,7 +17,6 @@
|
||||||
IBOutlet NSSearchField *searchField;
|
IBOutlet NSSearchField *searchField;
|
||||||
NSMetadataQuery *query;
|
NSMetadataQuery *query;
|
||||||
NSString *searchString;
|
NSString *searchString;
|
||||||
NSString *spotlightSearchPath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (IBAction)addToPlaylist:(id)sender;
|
- (IBAction)addToPlaylist:(id)sender;
|
||||||
|
@ -30,6 +29,5 @@
|
||||||
|
|
||||||
@property(retain) NSMetadataQuery *query;
|
@property(retain) NSMetadataQuery *query;
|
||||||
@property(copy) NSString *searchString;
|
@property(copy) NSString *searchString;
|
||||||
@property(copy) NSString *spotlightSearchPath;
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -42,16 +42,22 @@ static NSPredicate * musicOnlyPredicate = nil;
|
||||||
// Register value transformers
|
// Register value transformers
|
||||||
NSValueTransformer *pausingQueryTransformer = [[[PausingQueryTransformer alloc]init]autorelease];
|
NSValueTransformer *pausingQueryTransformer = [[[PausingQueryTransformer alloc]init]autorelease];
|
||||||
[NSValueTransformer setValueTransformer:pausingQueryTransformer forName:@"PausingQueryTransformer"];
|
[NSValueTransformer setValueTransformer:pausingQueryTransformer forName:@"PausingQueryTransformer"];
|
||||||
|
|
||||||
NSValueTransformer *authorToArtistTransformer = [[[AuthorToArtistTransformer alloc]init]autorelease];
|
NSValueTransformer *authorToArtistTransformer = [[[AuthorToArtistTransformer alloc]init]autorelease];
|
||||||
[NSValueTransformer setValueTransformer:authorToArtistTransformer forName:@"AuthorToArtistTransformer"];
|
[NSValueTransformer setValueTransformer:authorToArtistTransformer forName:@"AuthorToArtistTransformer"];
|
||||||
|
|
||||||
NSValueTransformer *pathToURLTransformer = [[[PathToURLTransformer alloc]init]autorelease];
|
NSValueTransformer *pathToURLTransformer = [[[PathToURLTransformer alloc]init]autorelease];
|
||||||
[NSValueTransformer setValueTransformer:pathToURLTransformer forName:@"PathToURLTransformers"];
|
[NSValueTransformer setValueTransformer:pathToURLTransformer forName:@"PathToURLTransformers"];
|
||||||
|
|
||||||
|
NSValueTransformer *stringToSearchScopeTransformer = [[[StringToSearchScopeTransformer alloc]init]autorelease];
|
||||||
|
[NSValueTransformer setValueTransformer:stringToSearchScopeTransformer forName:@"StringToSearchScopeTransformer"];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)init
|
- (id)init
|
||||||
{
|
{
|
||||||
if (self = [super initWithWindowNibName:@"SpotlightPanel"]) {
|
if (self = [super initWithWindowNibName:@"SpotlightPanel"]) {
|
||||||
self.query = [[NSMetadataQuery alloc] init];
|
self.query = [[[NSMetadataQuery alloc]init]autorelease];
|
||||||
[self.query setDelegate:self];
|
[self.query setDelegate:self];
|
||||||
self.query.sortDescriptors = [NSArray arrayWithObjects:
|
self.query.sortDescriptors = [NSArray arrayWithObjects:
|
||||||
[[NSSortDescriptor alloc]initWithKey:@"kMDItemAuthors"
|
[[NSSortDescriptor alloc]initWithKey:@"kMDItemAuthors"
|
||||||
|
@ -65,6 +71,17 @@ static NSPredicate * musicOnlyPredicate = nil;
|
||||||
selector:@selector(compareTrackNumbers:)],
|
selector:@selector(compareTrackNumbers:)],
|
||||||
nil];
|
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
|
// hook my query transformer up to me
|
||||||
[PausingQueryTransformer setSearchController:self];
|
[PausingQueryTransformer setSearchController:self];
|
||||||
}
|
}
|
||||||
|
@ -234,9 +251,8 @@ static NSPredicate * musicOnlyPredicate = nil;
|
||||||
|
|
||||||
- (void)dealloc
|
- (void)dealloc
|
||||||
{
|
{
|
||||||
[self.query stopQuery];
|
self.query = nil;
|
||||||
[self.query release];
|
self.searchString = nil;
|
||||||
[self.searchString release];
|
|
||||||
[musicOnlyPredicate release];
|
[musicOnlyPredicate release];
|
||||||
[super dealloc];
|
[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
|
@end
|
||||||
|
|
Loading…
Reference in New Issue