1/*
2 * This file is part of Krita
3 *
4 * Copyright (c) 2015 Algorithmus <angelus.tenebrae@gmail.com>
5 * Copyright (c) 2015 beelzy <alvina.lee@innoactive.de>
6 * Copyright (c) 2020 L. E. Segovia <amy@amyspark.me>
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; either version 2 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA.
22 */
23
24#include "UnzipTask.h"
25#import <Cocoa/Cocoa.h>
26#include <CoreFoundation/CoreFoundation.h>
27#include <CoreServices/CoreServices.h>
28#include <QuickLook/QuickLook.h>
29
30OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
31                               CFURLRef url, CFStringRef contentTypeUTI,
32                               CFDictionaryRef options);
33void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
34
35/* -----------------------------------------------------------------------------
36   Generate a preview for file
37
38   This function's job is to create preview for designated file
39   -----------------------------------------------------------------------------
40 */
41
42OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
43                               CFURLRef url, CFStringRef contentTypeUTI,
44                               CFDictionaryRef options) {
45  @autoreleasepool {
46    NSData *appPlist =
47        UnzipTask([(__bridge NSURL *)url path], @"mergedimage.png");
48
49    if (QLPreviewRequestIsCancelled(preview)) {
50      return noErr;
51    }
52
53    if ([appPlist length]) {
54      NSImage *appIcon = [[NSImage alloc] initWithData:appPlist];
55
56      NSImageRep *rep = [[appIcon representations] objectAtIndex:0];
57
58      NSSize canvasSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
59      NSRect renderRect = NSMakeRect(0.0, 0.0, rep.pixelsWide, rep.pixelsHigh);
60
61      CGContextRef _context =
62          QLPreviewRequestCreateContext(preview, canvasSize, true, NULL);
63
64      if (_context) {
65        NSGraphicsContext *_graphicsContext =
66            [NSGraphicsContext graphicsContextWithGraphicsPort:_context
67                                                       flipped:NO];
68
69        [NSGraphicsContext setCurrentContext:_graphicsContext];
70        [appIcon drawInRect:renderRect];
71
72        QLPreviewRequestFlushContext(preview, _context);
73        CFRelease(_context);
74      }
75
76      return noErr;
77    }
78
79    return NSFileNoSuchFileError;
80  }
81}
82
83void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview) {
84  // Implement only if supported
85}
86