This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratePreviewForURL.cpp
More file actions
77 lines (58 loc) · 3.15 KB
/
GeneratePreviewForURL.cpp
File metadata and controls
77 lines (58 loc) · 3.15 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <QuickLook/QuickLook.h>
#include "common.h"
#include "HTMLFormatter.h"
#include "MyCFUtilities.h"
/* -----------------------------------------------------------------------------
Generate a preview for file
This function's job is to create preview for designated file
----------------------------------------------------------------------------- */
#ifdef __cplusplus
extern "C" {
#endif
#pragma mark -
#pragma mark Preview generator function
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
static HTMLFormatter formatter; // ファイルを変換する C++ のクラス
CFStringRef HTML; // 変換した HTML
aslclient client = asl_open(BUNDLE_IDENTIFIER_CSTRING, NULL, ASL_OPT_STDERR); // ASL の Client, stderr に出力する
// とりあえず QLGENERATOR_BUNDLE_IDENTIFIER.plist の "Logging Performance Log" キーが true の場合に経過時間をロギングする。
// 環境変数で設定したほうがいいかも。とりあえず使用停止
// CFBooleanRef isLoggingPerformance = (CFBooleanRef)CFPreferencesCopyValue(CFSTR("Logging Preformance Log"), QLGENERATOR_BUNDLE_IDENTIFIER, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
asl_log(client, NULL, ASL_LEVEL_INFO, "UTI is %s", CFStringGetCStringPtr(contentTypeUTI, kCFStringEncodingUTF8));
if (QLPreviewRequestIsCancelled(preview)) goto cleanup;
formatter.setURL(url); // URL をセット
asl_log(client, NULL, ASL_LEVEL_INFO, "Convert to html start.");
HTML = formatter.htmlString(); // HTML に変換
if (HTML != NULL && !QLPreviewRequestIsCancelled(preview)) {
CFMutableDictionaryRef props = CFDictionaryCreateMutable(kCFAllocatorDefault, 4, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDataRef HTMLData = CreateDataFromString(HTML);
// Property をセット
CFDictionarySetValue(props, kQLPreviewPropertyMIMETypeKey , CFSTR("text/html"));
CFDictionarySetValue(props, kQLPreviewPropertyDisplayNameKey, formatter.threadTitle());
CFDictionarySetValue(props, kQLPreviewPropertyTextEncodingNameKey, CFSTR("UTF-8"));
CFDictionarySetValue(props, kQLPreviewPropertyAttachmentsKey, formatter.attachmentDictionary());
QLPreviewRequestSetDataRepresentation(preview, HTMLData, kUTTypeHTML, props); // Preview 開始
// デバッグ用途にhtmlに変換したデータをファイルに保存する。
#ifdef DEBUG
CFStringRef fileName = CreateFileNameFromURLWithExtension(url, CFSTR("html"));
CFStringWriteToTemporary((CFStringRef)HTML, fileName);
CFRelease(fileName);
#endif
CFRelease(props);
CFRelease(HTMLData);
}
// Preference のテストに使用。
// CFPreferencesSetValue(CFSTR("Logging Preformance Log"), (CFPropertyListRef)kCFBooleanTrue, QLGENERATOR_BUNDLE_IDENTIFIER, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
// CFPreferencesSynchronize(QLGENERATOR_BUNDLE_IDENTIFIER, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
cleanup:
asl_close(client);
return noErr;
}
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
// implement only if supported
}
#ifdef __cplusplus
}
#endif