Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
GeneratePreviewForURL.m
Go to the documentation of this file.
1#include <CoreFoundation/CoreFoundation.h>
2#include <CoreServices/CoreServices.h>
3#include <QuickLook/QuickLook.h>
4#include <libgen.h>
5
6#import <Cocoa/Cocoa.h>
7
8#include "ReadFile.h"
9
10/* -----------------------------------------------------------------------------
11 Generate a preview for a ROOT file
12 ----------------------------------------------------------------------------- */
13
14OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
15{
16 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
17
18#ifdef DEBUG
19 NSDate *startDate = [NSDate date];
20#endif
21
22 // Get the posix-style path for the thing we are quicklooking at
23 NSString *fullPath = (NSString*)CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
24
25#ifdef DEBUG
26 NSLog(@"GeneratePreviewForURL %@", fullPath);
27#endif
28
29 // Check for cancel
30 if (QLPreviewRequestIsCancelled(preview)) {
31 [pool release];
32 return noErr;
33 }
34
35 // Set properties for the preview data
36 NSMutableDictionary *props = [[[NSMutableDictionary alloc] init] autorelease];
37 [props setObject: @"UTF-8" forKey: (NSString *)kQLPreviewPropertyTextEncodingNameKey];
38 [props setObject: @"text/html" forKey: (NSString *)kQLPreviewPropertyMIMETypeKey];
39 //[props setObject: [NSString stringWithFormat: @"Contents of %@", fullPath] forKey: (NSString *)kQLPreviewPropertyDisplayNameKey];
40 [props setObject: [NSString stringWithFormat: @"Contents of %s", basename((char*)[fullPath UTF8String])] forKey: (NSString *)kQLPreviewPropertyDisplayNameKey];
41
42 // Build the HTML
43 NSMutableString *html = [[[NSMutableString alloc] init] autorelease];
44 [html appendString: @"<html>"];
45 [html appendString: @"<head><style type=\"text/css\">"];
46 [html appendString: @"body, td, th, p, div { font-family: Arial, Helvetica, sans-serif; font-size: 12px }"];
47 [html appendString: @"</style></head>"];
48 [html appendString: @"<body bgcolor=white>"];
49
50 // Read ROOT file and fill html
51 if (ReadFile(fullPath, html, preview) == -1) {
52 [pool release];
53 return noErr;
54 }
55
56 [html appendString: @"</body></html>"];
57
58#ifdef DEBUG
59 NSLog(@"Scanned file %@ in %.3f sec",
60 fullPath, -[startDate timeIntervalSinceNow]);
61#endif
62
63 // Check for cancel
64 if (QLPreviewRequestIsCancelled(preview)) {
65 [pool release];
66 return noErr;
67 }
68
69 // Now let WebKit do its thing
70 QLPreviewRequestSetDataRepresentation(preview, (CFDataRef)[html dataUsingEncoding: NSUTF8StringEncoding], kUTTypeHTML, (CFDictionaryRef)props);
71
72 [pool release];
73 return noErr;
74}
75
76void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
77{
78 // implement only if supported
79}
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview)
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
int ReadFile(NSString *fullPath, NSMutableString *html, QLPreviewRequestRef preview)
Definition ReadFile.m:461