GeneratePreviewForURL.m

Go to the documentation of this file.
00001 #include <CoreFoundation/CoreFoundation.h>
00002 #include <CoreServices/CoreServices.h>
00003 #include <QuickLook/QuickLook.h>
00004 #include <libgen.h>
00005 
00006 #import <Cocoa/Cocoa.h>
00007 
00008 #include "ReadFile.h"
00009 
00010 /* -----------------------------------------------------------------------------
00011    Generate a preview for a ROOT file
00012    ----------------------------------------------------------------------------- */
00013 
00014 OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
00015 {
00016    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00017 
00018 #ifdef DEBUG
00019    NSDate *startDate = [NSDate date];
00020 #endif
00021 
00022         // Get the posix-style path for the thing we are quicklooking at
00023         NSString *fullPath = (NSString*)CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
00024 
00025 #ifdef DEBUG
00026    NSLog(@"GeneratePreviewForURL %@", fullPath);
00027 #endif
00028 
00029    // Check for cancel
00030         if (QLPreviewRequestIsCancelled(preview)) {
00031                 [pool release];
00032                 return noErr;
00033         }
00034 
00035    // Set properties for the preview data
00036         NSMutableDictionary *props = [[[NSMutableDictionary alloc] init] autorelease];
00037    [props setObject: @"UTF-8" forKey: (NSString *)kQLPreviewPropertyTextEncodingNameKey];
00038    [props setObject: @"text/html" forKey: (NSString *)kQLPreviewPropertyMIMETypeKey];
00039         //[props setObject: [NSString stringWithFormat: @"Contents of %@", fullPath] forKey: (NSString *)kQLPreviewPropertyDisplayNameKey];
00040    [props setObject: [NSString stringWithFormat: @"Contents of %s", basename((char*)[fullPath UTF8String])] forKey: (NSString *)kQLPreviewPropertyDisplayNameKey];
00041 
00042         // Build the HTML
00043    NSMutableString *html = [[[NSMutableString alloc] init] autorelease];
00044    [html appendString: @"<html>"];
00045    [html appendString: @"<head><style type=\"text/css\">"];
00046    [html appendString: @"body, td, th, p, div { font-family: Arial, Helvetica, sans-serif; font-size: 12px }"];
00047    [html appendString: @"</style></head>"];
00048    [html appendString: @"<body bgcolor=white>"];
00049 
00050    // Read ROOT file and fill html
00051    if (ReadFile(fullPath, html, preview) == -1) {
00052       [pool release];
00053       return noErr;
00054    }
00055 
00056    [html appendString: @"</body></html>"];
00057 
00058 #ifdef DEBUG
00059    NSLog(@"Scanned file %@ in %.3f sec",
00060          fullPath, -[startDate timeIntervalSinceNow]);
00061 #endif
00062 
00063    // Check for cancel
00064         if (QLPreviewRequestIsCancelled(preview)) {
00065                 [pool release];
00066                 return noErr;
00067         }
00068 
00069    // Now let WebKit do its thing
00070    QLPreviewRequestSetDataRepresentation(preview, (CFDataRef)[html dataUsingEncoding: NSUTF8StringEncoding], kUTTypeHTML, (CFDictionaryRef)props);
00071 
00072    [pool release];
00073    return noErr;
00074 }
00075 
00076 void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
00077 {
00078    // implement only if supported
00079 }

Generated on Tue Jul 5 14:44:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1