This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mdfindwrapper.mm
52 lines (44 loc) · 1.49 KB
/
mdfindwrapper.mm
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
#import <sys/types.h>
#import <unistd.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#import <QtCore/QtCore>
#import <QString>
bool QuickGetFilesByFileName(const QString& fileName, QStringList& results)
{
QString queryString = QString("* == \"%1\"wcd || kMDItemFSName = \"%1\"c").arg(fileName);
CFStringRef rawQuery = CFStringCreateWithCString(kCFAllocatorDefault,
queryString.toStdString().c_str(),
CFStringGetSystemEncoding());
if (!rawQuery)
{
qDebug() << "creating CFString failed";
return false;
}
MDQueryRef queryRef = MDQueryCreate(kCFAllocatorDefault, rawQuery, NULL, NULL);
if (!queryRef)
{
qDebug() << "creating MDQuery failed";
CFRelease(rawQuery);
return false;
}
// go execute the query
if (!MDQueryExecute(queryRef, kMDQuerySynchronous))
{
qDebug() << "MDQueryExecute failed";
CFRelease(rawQuery);
CFRelease(queryRef);
return false;
}
int count = MDQueryGetResultCount(queryRef);
for (int i = 0; i < count; i++)
{
MDItemRef item = (MDItemRef)MDQueryGetResultAtIndex(queryRef, i);
NSString* name = (NSString*)MDItemCopyAttribute(item, kMDItemPath);
results.append( QString::fromNSString(name));
[name autorelease];
}
CFRelease(rawQuery);
CFRelease(queryRef);
return !results.isEmpty();
}