#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>

#import <Foundation/Foundation.h>

//START:GenerateThumbnailForURL
OSStatus GenerateThumbnailForURL(void *thisInterface, 
                                 QLThumbnailRequestRef thumbnail, 
                                 CFURLRef url, 
                                 CFStringRef contentTypeUTI, 
                                 CFDictionaryRef options, 
                                 CGSize maxSize)
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  @try {
    NSDictionary *metadata;
    metadata = [NSDictionary dictionaryWithContentsOfURL:(NSURL*)url];
    NSString *pathToImage = [metadata valueForKey:@"kPPImagePath"];
    if (!pathToImage) { 
      //No image available
      return noErr;
    }
    NSData *imageData = [NSData dataWithContentsOfFile:pathToImage];
    if (!imageData) {
      //Unable to load the data for some reason.
      return noErr;
    }
    
    QLThumbnailRequestSetImageWithData(thumbnail, (CFDataRef)imageData, NULL);
  } @finally {
    [pool release], pool = nil;
  }
  return noErr;
}
//END:GenerateThumbnailForURL

void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
{
    // implement only if supported
}
