FreeCAD

Форк
0
/
GenerateThumbnailForURL.m 
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
/* -----------------------------------------------------------------------------
8
    Generate a thumbnail for file
9

10
   This function's job is to create thumbnail for designated file as fast as possible
11
   ----------------------------------------------------------------------------- */
12

13
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
14
{
15
  OSStatus 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

27
    NSUUID *uuid = [NSUUID UUID];
28
    NSString *uuidPath = [uuid UUIDString];
29
    NSString *unzipDstPath = [NSString stringWithFormat:@"%@/%@/", NSTemporaryDirectory(), uuidPath];
30
    NSString *unzipDstFile = [NSString stringWithFormat:@"%@%@", unzipDstPath, @"Thumbnail.png"];
31
    NSURL *zipFile = (__bridge NSURL *)url;
32
    NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip"
33
                                            arguments:@[@"-qq", @"-o", @"-j", @"-d", unzipDstPath, [zipFile path], @"thumbnails/Thumbnail.png"]];
34
    [task waitUntilExit];
35
    //        NSLog(@"%@", unzipDstPath);
36
    //        NSLog(@"%@", unzipDstFile);
37

38
    if ( [[NSFileManager defaultManager] fileExistsAtPath:unzipDstFile] )
39
    {
40
      CGSize canvasSize = CGSizeMake(128, 128);
41
      CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, /*maxSize*/canvasSize, true, NULL);
42
      if(cgContext)
43
      {
44
        CGDataProviderRef pngDP = CGDataProviderCreateWithFilename([unzipDstFile fileSystemRepresentation]);
45
        CGImageRef image = CGImageCreateWithPNGDataProvider(pngDP, NULL, true, kCGRenderingIntentDefault);
46
        CGDataProviderRelease(pngDP);
47

48
        CGContextDrawImage(cgContext,CGRectMake(0, 0, 128, 128), image);
49

50
        QLThumbnailRequestFlushContext(thumbnail, cgContext);
51
        ret = noErr;
52

53
        CFRelease(cgContext);
54
        CFRelease(image);
55
      }
56
    }
57

58
    if ( [[NSFileManager defaultManager] isDeletableFileAtPath:unzipDstFile] )
59
      [[NSFileManager defaultManager] removeItemAtPath:unzipDstFile error:nil];
60

61
    if ( [[NSFileManager defaultManager] isDeletableFileAtPath:unzipDstPath] )
62
      [[NSFileManager defaultManager] removeItemAtPath:unzipDstPath error:nil];
63
  }
64

65
  return ret;
66
}
67

68

69
void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
70
{
71
    // implement only if supported
72
}
73

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.