forked from dabrahams/qlrest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GeneratePreviewForURL.m
32 lines (27 loc) · 1.16 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
#import <QuickLook/QuickLook.h>
#import <Cocoa/Cocoa.h>
#include "RestConverter.h"
/* -----------------------------------------------------------------------------
Generate a preview for file
This function's job is to create preview for designated file
-------------------------------------------------------------------------- */
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
CFURLRef url, CFStringRef contentTypeUTI,
CFDictionaryRef options)
{
// TODO: Fallback to plaintext rendering if rest rendering fails?
NSString *source = [NSString stringWithContentsOfFile:[(NSURL*)url path] encoding:NSUTF8StringEncoding error:nil];
CFDataRef data = (CFDataRef) renderRest(source);
CFStringRef uttype = kUTTypeHTML;
if (!data) {
data = (CFDataRef) [source dataUsingEncoding:NSUTF8StringEncoding];
uttype = kUTTypePlainText;
}
CFDictionaryRef props = (CFDictionaryRef) [NSDictionary dictionary];
QLPreviewRequestSetDataRepresentation(preview, data, uttype, props);
return noErr;
}
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
// implement only if supported
}