FreeCAD
72 строки · 2.9 Кб
1#include <CoreFoundation/CoreFoundation.h>
2#include <CoreServices/CoreServices.h>
3#include <QuickLook/QuickLook.h>
4#include <Foundation/Foundation.h>
5#include <AppKit/AppKit.h>
6
7/* -----------------------------------------------------------------------------
8Generate a thumbnail for file
9
10This function's job is to create thumbnail for designated file as fast as possible
11----------------------------------------------------------------------------- */
12
13OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
14{
15OSStatus ret = coreFoundationUnknownErr;
16
17@autoreleasepool {
18
19// unzip -qq -o -j -d /tmp ~/test.FCStd thumbnails/Thumbnail.png
20// -qq : be really quiet
21// -o : overwrite without prompt
22// -j : don't create archive paths
23// -d : destination path (create this path)
24// extracts thumbnails/Thumbnail.png to /tmp/Thumbnail.png .
25// We add a UUID and use a system temp directory here.
26
27NSUUID *uuid = [NSUUID UUID];
28NSString *uuidPath = [uuid UUIDString];
29NSString *unzipDstPath = [NSString stringWithFormat:@"%@/%@/", NSTemporaryDirectory(), uuidPath];
30NSString *unzipDstFile = [NSString stringWithFormat:@"%@%@", unzipDstPath, @"Thumbnail.png"];
31NSURL *zipFile = (__bridge NSURL *)url;
32NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip"
33arguments:@[@"-qq", @"-o", @"-j", @"-d", unzipDstPath, [zipFile path], @"thumbnails/Thumbnail.png"]];
34[task waitUntilExit];
35// NSLog(@"%@", unzipDstPath);
36// NSLog(@"%@", unzipDstFile);
37
38if ( [[NSFileManager defaultManager] fileExistsAtPath:unzipDstFile] )
39{
40CGSize canvasSize = CGSizeMake(128, 128);
41CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, /*maxSize*/canvasSize, true, NULL);
42if(cgContext)
43{
44CGDataProviderRef pngDP = CGDataProviderCreateWithFilename([unzipDstFile fileSystemRepresentation]);
45CGImageRef image = CGImageCreateWithPNGDataProvider(pngDP, NULL, true, kCGRenderingIntentDefault);
46CGDataProviderRelease(pngDP);
47
48CGContextDrawImage(cgContext,CGRectMake(0, 0, 128, 128), image);
49
50QLThumbnailRequestFlushContext(thumbnail, cgContext);
51ret = noErr;
52
53CFRelease(cgContext);
54CFRelease(image);
55}
56}
57
58if ( [[NSFileManager defaultManager] isDeletableFileAtPath:unzipDstFile] )
59[[NSFileManager defaultManager] removeItemAtPath:unzipDstFile error:nil];
60
61if ( [[NSFileManager defaultManager] isDeletableFileAtPath:unzipDstPath] )
62[[NSFileManager defaultManager] removeItemAtPath:unzipDstPath error:nil];
63}
64
65return ret;
66}
67
68
69void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
70{
71// implement only if supported
72}
73