-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneratePreviewForURL.m
106 lines (88 loc) · 3.54 KB
/
GeneratePreviewForURL.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <Foundation/Foundation.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
/* -----------------------------------------------------------------------------
Generate a preview for file
This function's job is to create preview for designated file
----------------------------------------------------------------------------- */
NSString *FindHsColour()
{
NSArray *searchDirs;
NSString *homeDir;
NSFileManager *fm;
NSBundle *mainBundle;
fm = [NSFileManager defaultManager];
// 1. If configuration variable is set, use it. XXX: implement this last.
// 2. If it's in any one of a predefined list of paths, use it. (Search in order)
homeDir = NSHomeDirectory();
searchDirs = [NSArray arrayWithObjects:
@"/usr/local/bin/",
@"/opt/local/bin/",
@"/sw/bin/",
[homeDir stringByAppendingString: @"/.cabal/bin/"],
[homeDir stringByAppendingString: @"/bin/"],
nil ];
for(NSString *dir in searchDirs) {
NSString *execPath = [dir stringByAppendingString: @"HsColour"];
//NSLog(@"HsColour path: %@", execPath);
if([fm isExecutableFileAtPath: execPath]) {
//NSLog(@"Found");
return execPath;
}
//NSLog(@"Not found");
}
// 3. If it's in the application bundle, use it from there.
mainBundle = [NSBundle mainBundle];
if(mainBundle != nil) {
return [mainBundle pathForAuxiliaryExecutable: @"HsColour"];
}
// 4. Otherwise, return nil to indicate that HsColour can't be found.
return nil;
}
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url,
CFStringRef contentTypeUTI, CFDictionaryRef options)
{
NSAutoreleasePool *pool;
NSMutableDictionary *props;
CFStringRef fileRef;
NSTask *hsColour;
id htmlReader;
NSData *htmlData;
NSPipe *pipe;
NSString *hsColourPath;
pool = [[NSAutoreleasePool alloc] init];
if (QLPreviewRequestIsCancelled(preview))
return noErr;
hsColourPath = FindHsColour();
if (QLPreviewRequestIsCancelled(preview))
return noErr;
if(hsColourPath != nil) {
fileRef = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
hsColour = [[[NSTask alloc] init] autorelease];
[hsColour setLaunchPath: FindHsColour()];
[hsColour setArguments: [NSArray arrayWithObjects: @"-html", (NSString *)fileRef, nil]];
pipe = [[[NSPipe alloc] init] autorelease];
[hsColour setStandardOutput: pipe];
[hsColour launch];
htmlReader = [pipe fileHandleForReading];
htmlData = [htmlReader readDataToEndOfFile];
CFRelease(fileRef);
} else {
NSString *notFound = @"<p>HsColour not found</p>";
htmlData = [notFound dataUsingEncoding: NSUTF8StringEncoding];
}
if (QLPreviewRequestIsCancelled(preview))
return noErr;
props=[[[NSMutableDictionary alloc] init] autorelease];
[props setObject:@"UTF-8" forKey:(NSString *)kQLPreviewPropertyTextEncodingNameKey];
[props setObject:@"text/html" forKey:(NSString *)kQLPreviewPropertyMIMETypeKey];
QLPreviewRequestSetDataRepresentation(preview, (CFDataRef)htmlData,
kUTTypeHTML, (CFDictionaryRef)props);
[pool release];
return noErr;
}
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
// implement only if supported
}