-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQSMDFindWrapper.m
87 lines (70 loc) · 2.8 KB
/
QSMDFindWrapper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// QSMDFindWrapper.m
// QSSpotlightPlugIn
//
// Created by Nicholas Jitkoff on 3/21/05.
// Copyright 2005 __MyCompanyName__. All rights reserved.
//
#import "QSMDFindWrapper.h"
@implementation QSMDFindWrapper
+ (QSMDFindWrapper *)findWrapperWithQuery:(NSString *)query path:(NSString *)path keepalive:(BOOL)flag{
return [[self alloc] initWithQuery:(NSString *)query path:(NSString *)path keepalive:(BOOL)flag];
}
- (id)initWithQuery:(NSString *)aQuery path:(NSString *)aPath keepalive:(BOOL)flag{
if (self=[super init]){
results=[[NSMutableArray alloc]init];
resultPaths=[[NSMutableString alloc]init];
path=aPath;
query=aQuery;
keepalive=flag;
}
return self;
}
- (NSMutableArray *)results
{
return results;
}
- (void)startQuery
{
task = [NSTask taskWithLaunchPath:@"/usr/bin/mdfind" arguments:[NSArray arrayWithObjects:query, path ? @"-onlyin" : nil, path, nil]];
[task setStandardOutput:[NSPipe pipe]];
NSFileHandle *handle = [[task standardOutput]fileHandleForReading];
[task launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataAvailable:) name:NSFileHandleReadCompletionNotification object:handle];
[handle readInBackgroundAndNotify];
// results=[[NSMutableArray alloc]init];
// QSObject *searchObject=[QSObject objectWithString:@"Searching"];
// [searchObject setIcon:[QSResourceManager imageNamed:@"Find"]];
// [results addObject:searchObject];
QSTask *spotlightTask = [QSTasks taskWithIdentifier:@"QSSpotlight"];
[spotlightTask setStatus:@"Performing Search"];
[spotlightTask setProgress:0];
//return results;
}
-(void)dataAvailable:(NSNotification *)notif{
NSFileHandle *handle=[notif object];
//return;
NSData *data=[[notif userInfo]
objectForKey: NSFileHandleNotificationDataItem];
NSString *newString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
if ([newString length])
[resultPaths appendString:newString];
NSArray *pathArray=[resultPaths componentsSeparatedByString:@"\n"];
[resultPaths setString:[pathArray lastObject]];
pathArray=[pathArray subarrayWithRange:NSMakeRange(0,[pathArray count]-1)];
// NSLog(@"paths %d",[pathArray count]);
//NSLog(@"remaining:%@",resultPaths);
[results addObjectsFromArray:[QSObject fileObjectsWithPathArray:pathArray]];
// NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:results, kQSResultArrayKey, nil];
// [[NSNotificationCenter defaultCenter] postNotificationName:@"QSSourceArrayUpdated" object:self userInfo:userInfo];
// [results release];
// results=nil;
if ([data length])
[handle readInBackgroundAndNotify];
else{
[[QSTasks taskWithIdentifier:@"QSSpotlight"] stop];
// [results removeObjectAtIndex:0];
// [[NSNotificationCenter defaultCenter]postNotificationName:@"QSSourceArrayUpdated" object:self userInfo:userInfo];
}
}
@end