Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////AppController.h////////////
- #import <Cocoa/Cocoa.h>
- @interface AppController : NSObject <NSMetadataQueryDelegate> {
- NSMetadataQuery *query;
- NSString *searchKey;
- BOOL searchContent;
- }
- - (void)queryNote:(NSNotification *)note;
- - (void)setSearchText:(NSString *)text;
- @property(retain) NSMetadataQuery *query;
- @property(copy) NSString *searchKey;
- @property BOOL searchContent;
- @end
- ////////////AppController.m////////////
- #import "AppController.h"
- @implementation AppController
- + (void)initialize {
- //Doing nothing for now
- }
- - (id)init {
- if (self = [super init]) {
- self.searchKey = @"";
- self.query = [[[NSMetadataQuery alloc] init] autorelease];
- NSNotificationCenter *nf = [NSNotificationCenter defaultCenter];
- [nf addObserver:self selector:@selector(queryNote:) name:nil object:self.query];
- [self.query setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemFSName ascending:YES] autorelease]]];
- [self.query setGroupingAttributes:[NSArray arrayWithObjects:(id)kMDItemKind, (id)kMDItemFSSize, nil]];
- [self.query setDelegate:self];
- }
- return self;
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [query release];
- [searchKey release];
- [super dealloc];
- }
- @synthesize query;
- - (void)queryNote:(NSNotification *)note {
- NSLog(@"%@",note.name);
- }
- - (void)createSearchPredicate {
- NSPredicate *predicateToRun = nil;
- if (self.searchContent) {
- NSString *predicateFormat = @"kMDItemTextContent like[c] %@";
- predicateToRun = [NSPredicate predicateWithFormat:predicateFormat, self.searchKey];
- }
- NSUInteger options = (NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption);
- NSPredicate *compPred = [NSComparisonPredicate
- predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"*"]
- rightExpression:[NSExpression expressionForConstantValue:self.searchKey]
- modifier:NSDirectPredicateModifier
- type:NSLikePredicateOperatorType
- options:options];
- if (self.searchContent) {
- predicateToRun = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:compPred, predicateToRun, nil]];
- } else {
- predicateToRun = compPred;
- }
- NSPredicate *emailExclusionPredicate = [NSPredicate predicateWithFormat:@"(kMDItemContentType != 'com.apple.mail.emlx') && (kMDItemContentType != 'public.vcard')"];
- predicateToRun = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicateToRun, emailExclusionPredicate, nil]];
- [self.query setPredicate:predicateToRun];
- [self.query startQuery];
- }
- - (void)setSearchContent:(BOOL)value {
- if (searchContent != value) {
- searchContent = value;
- [self createSearchPredicate];
- }
- }
- - (BOOL)searchContent {
- return searchContent;
- }
- - (void)setSearchText:(NSString *)text
- {
- self.searchKey = text;
- }
- - (void)setSearchKey:(NSString *) value {
- if (searchKey != value) {
- NSLog(value);
- [searchKey release];
- searchKey = [value copy];
- [self createSearchPredicate];
- }
- }
- - (NSString *)searchKey {
- return [[searchKey retain] autorelease];
- }
- - (void)tableViewDoubleClick:(id)path {
- [[NSWorkspace sharedWorkspace] openFile:path];
- }
- @end
- ////////////main.cpp////////////
- AppController* testObj = [[AppController alloc] init];
- [testObj setSearchText:@"*.txt"];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement