|
1 | 1 | #include <CoreFoundation/CoreFoundation.h> |
2 | 2 | #include <CoreServices/CoreServices.h> |
3 | 3 | #include <QuickLook/QuickLook.h> |
| 4 | +#include <WebKit/WebKit.h> |
| 5 | +#import "Common.h" |
| 6 | + |
| 7 | +#define minSize 32 |
4 | 8 |
|
5 | 9 | /* ----------------------------------------------------------------------------- |
6 | 10 | Generate a thumbnail for file |
7 | 11 |
|
8 | 12 | This function's job is to create thumbnail for designated file as fast as possible |
9 | 13 | ----------------------------------------------------------------------------- */ |
10 | 14 |
|
11 | | -OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) |
| 15 | +OSStatus GenerateThumbnailForURL(void *thisInterface, |
| 16 | + QLThumbnailRequestRef thumbnail, |
| 17 | + CFURLRef url, |
| 18 | + CFStringRef contentTypeUTI, |
| 19 | + CFDictionaryRef options, |
| 20 | + CGSize maxSize) |
12 | 21 | { |
| 22 | + n8log(@"Generating Thumbnail"); |
| 23 | + // For some reason we seem to get called for small thumbnails even though |
| 24 | + // we put a min size in our .plist file... |
| 25 | + if (maxSize.width < minSize || maxSize.height < minSize) |
| 26 | + return noErr; |
| 27 | + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 28 | + |
| 29 | + // Render as though there is an 600x800 window, and fill the thumbnail |
| 30 | + // vertically. This code could be more general. I'm assuming maxSize is |
| 31 | + // a square, though nothing horrible should happen if it isn't. |
| 32 | + |
| 33 | + NSRect renderRect = NSMakeRect(0.0, 0.0, 600.0, 800.0); |
| 34 | + float scale = maxSize.height/800.0; |
| 35 | + NSSize scaleSize = NSMakeSize(scale, scale); |
| 36 | + CGSize thumbSize = NSSizeToCGSize( |
| 37 | + NSMakeSize((maxSize.width * (600.0/800.0)), |
| 38 | + maxSize.height)); |
| 39 | + |
| 40 | + /* Based on example code from quicklook-dev mailing list */ |
| 41 | + // NSSize previewSize = NSSizeFromCGSize(maxSize); |
| 42 | + int status; |
| 43 | + CFBundleRef bundle = QLThumbnailRequestGetGeneratorBundle(thumbnail); |
| 44 | + NSData *data = colorizeURL(bundle, url, &status, 1); |
| 45 | + //NSLog(@"%s", [data bytes]); |
| 46 | + if (status != 0) { |
| 47 | + goto done; |
| 48 | + } |
| 49 | + //NSRect previewRect; |
| 50 | + //previewRect.size = previewSize; |
| 51 | + |
| 52 | + WebView* webView = [[WebView alloc] initWithFrame:renderRect]; |
| 53 | + [webView scaleUnitSquareToSize:scaleSize]; |
| 54 | + [[[webView mainFrame] frameView] setAllowsScrolling:NO]; |
| 55 | + |
| 56 | + [[webView mainFrame] loadData:data MIMEType:@"text/html" |
| 57 | + textEncodingName:@"UTF-8" baseURL:nil]; |
| 58 | + |
| 59 | + while([webView isLoading]) { |
| 60 | + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); |
| 61 | + } |
| 62 | + |
| 63 | + // Get a context to render into |
| 64 | + CGContextRef context = |
| 65 | + QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL); |
| 66 | + |
| 67 | + if(context != NULL) { |
| 68 | + NSGraphicsContext* nsContext = |
| 69 | + [NSGraphicsContext |
| 70 | + graphicsContextWithGraphicsPort:(void *)context |
| 71 | + flipped:[webView isFlipped]]; |
| 72 | + |
| 73 | + [webView displayRectIgnoringOpacity:[webView bounds] |
| 74 | + inContext:nsContext]; |
| 75 | + |
| 76 | + QLThumbnailRequestFlushContext(thumbnail, context); |
| 77 | + |
| 78 | + CFRelease(context); |
| 79 | + } |
| 80 | +done: |
| 81 | + [pool release]; |
13 | 82 | return noErr; |
14 | 83 | } |
15 | 84 |
|
16 | | -void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail) |
| 85 | +void CancelThumbnailGeneration(void* thisInterface, |
| 86 | + QLThumbnailRequestRef thumbnail) |
17 | 87 | { |
18 | 88 | // implement only if supported |
19 | 89 | } |
0 commit comments