diff options
author | rowanbeentje <rowan@beent.je> | 2010-08-04 22:24:30 +0000 |
---|---|---|
committer | rowanbeentje <rowan@beent.je> | 2010-08-04 22:24:30 +0000 |
commit | d20d35aedb551516e1974979e8a0048a0ea81cbf (patch) | |
tree | 444f242d8411489960290e60d89b89685a555832 /Source | |
parent | 71002662ef004b798766906bce9e51ad359e1745 (diff) | |
download | sequelpro-d20d35aedb551516e1974979e8a0048a0ea81cbf.tar.gz sequelpro-d20d35aedb551516e1974979e8a0048a0ea81cbf.tar.bz2 sequelpro-d20d35aedb551516e1974979e8a0048a0ea81cbf.zip |
- Add an initial Sequel Pro QuickLook plugin, written by bibiko, as a build dependency
Diffstat (limited to 'Source')
-rw-r--r-- | Source/GeneratePreviewForURL.m | 347 | ||||
-rw-r--r-- | Source/GenerateThumbnailForURL.m | 86 | ||||
-rw-r--r-- | Source/main.c | 218 |
3 files changed, 651 insertions, 0 deletions
diff --git a/Source/GeneratePreviewForURL.m b/Source/GeneratePreviewForURL.m new file mode 100644 index 00000000..30ca2ae2 --- /dev/null +++ b/Source/GeneratePreviewForURL.m @@ -0,0 +1,347 @@ +// GeneratePreviewForURL.m +// sequel-pro +// +// Created by Hans-Jörg Bibiko on Aug 04, 2010 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at <http://code.google.com/p/sequel-pro/> + +#include <CoreFoundation/CoreFoundation.h> +#include <CoreServices/CoreServices.h> +#include <QuickLook/QuickLook.h> +#import <Cocoa/Cocoa.h> + +/* ----------------------------------------------------------------------------- + Generate a preview for file + + This function's job is to create preview for designated file + ----------------------------------------------------------------------------- */ + +static char base64encodingTable[64] = { +'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', +'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', +'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', +'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' }; + +@interface NSData (QLDataAdditions) + +- (NSString *)base64EncodingWithLineLength:(NSUInteger)lineLength; + +@end + + +@implementation NSData (QLDataAdditions) + +/* + * Derived from http://colloquy.info/project/browser/trunk/NSDataAdditions.m?rev=1576 + * Created by khammond on Mon Oct 29 2001. + * Formatted by Timothy Hatcher on Sun Jul 4 2004. + * Copyright (c) 2001 Kyle Hammond. All rights reserved. + * Original development by Dave Winer. + * + * Convert self to a base64 encoded NSString + */ +- (NSString *) base64EncodingWithLineLength:(NSUInteger)lineLength { + + const unsigned char *bytes = [self bytes]; + NSUInteger ixtext = 0; + NSUInteger lentext = [self length]; + NSInteger ctremaining = 0; + unsigned char inbuf[3], outbuf[4]; + short i = 0; + short charsonline = 0, ctcopy = 0; + NSUInteger ix = 0; + + NSMutableString *base64 = [NSMutableString stringWithCapacity:lentext]; + + while(1) { + ctremaining = lentext - ixtext; + if( ctremaining <= 0 ) break; + + for( i = 0; i < 3; i++ ) { + ix = ixtext + i; + if( ix < lentext ) inbuf[i] = bytes[ix]; + else inbuf [i] = 0; + } + + outbuf [0] = (inbuf [0] & 0xFC) >> 2; + outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4); + outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6); + outbuf [3] = inbuf [2] & 0x3F; + ctcopy = 4; + + switch( ctremaining ) { + case 1: + ctcopy = 2; + break; + case 2: + ctcopy = 3; + break; + } + + for( i = 0; i < ctcopy; i++ ) + [base64 appendFormat:@"%c", base64encodingTable[outbuf[i]]]; + + for( i = ctcopy; i < 4; i++ ) + [base64 appendFormat:@"%c",'=']; + + ixtext += 3; + charsonline += 4; + + if( lineLength > 0 ) { + if (charsonline >= lineLength) { + charsonline = 0; + [base64 appendString:@"\n"]; + } + } + } + + return base64; +} +@end + +@interface NSString (QLStringAdditions) + ++ (NSString *)stringForByteSize:(long long)byteSize; + +@end + +@implementation NSString (QLStringAdditions) + +/* + * Returns a human readable version string of the supplied byte size. + */ ++ (NSString *)stringForByteSize:(long long)byteSize +{ + CGFloat size = byteSize; + + NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease]; + + [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; + + if (size < 1023) { + [numberFormatter setFormat:@"#,##0 B"]; + + return [numberFormatter stringFromNumber:[NSNumber numberWithInteger:size]]; + } + + size = (size / 1024); + + if (size < 1023) { + [numberFormatter setFormat:@"#,##0.0 KiB"]; + + return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:size]]; + } + + size = (size / 1024); + + if (size < 1023) { + [numberFormatter setFormat:@"#,##0.0 MiB"]; + + return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:size]]; + } + + size = (size / 1024); + + if (size < 1023) { + [numberFormatter setFormat:@"#,##0.0 GiB"]; + + return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:size]]; + } + + size = (size / 1024); + + [numberFormatter setFormat:@"#,##0.0 TiB"]; + + return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:size]]; +} +@end + +OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options) +{ + + NSURL *myURL = (NSURL *)url; + NSString *urlExtension = [[[myURL path] pathExtension] lowercaseString]; + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + + NSError *templateReadError = nil; + + NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:[myURL path]]; + + NSMutableString *html; + NSString *template = nil; + + if (false == QLPreviewRequestIsCancelled(preview)) { + + NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[myURL path] error:nil]; + + if([urlExtension isEqualToString:@"spf"]) { + + NSError *readError = nil; + NSString *convError = nil; + NSPropertyListFormat format; + NSDictionary *spf = nil; + + NSData *pData = [NSData dataWithContentsOfFile:[myURL path] options:NSUncachedRead error:&readError]; + + spf = [[NSPropertyListSerialization propertyListFromData:pData + mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&convError] retain]; + + if(!spf || readError != nil || [convError length] || !(format == NSPropertyListXMLFormat_v1_0 || format == NSPropertyListBinaryFormat_v1_0)) { + if(spf) [spf release]; + [pool release]; + return noErr; + } + + if([[spf objectForKey:@"format"] isEqualToString:@"connection"]) { + template = [NSString stringWithContentsOfFile:[[NSBundle bundleWithIdentifier:@"com.google.code.sequel-pro.qlgenerator"] pathForResource:@"SPQLPluginConnectionTemplate" ofType:@"html"] + encoding:NSUTF8StringEncoding error:&templateReadError]; + + if (template == nil || ![template length] || templateReadError != nil) { + [pool release]; + return noErr; + } + + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; + [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; + [dateFormatter setLocale:[NSLocale currentLocale]]; + + NSString *name = @"••••"; + NSString *host = @"••••"; + NSString *user = @"••••"; + NSString *database = @"••••"; + NSString *autoConnect = ([[spf objectForKey:@"auto_connect"] boolValue]) ? @"checked" : @""; + + if([[spf objectForKey:@"data"] isKindOfClass:[NSDictionary class]]) { + if([[spf objectForKey:@"data"] objectForKey:@"connection"] && [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"name"]) + name = [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"name"]; + else + name = @""; + if([[spf objectForKey:@"data"] objectForKey:@"connection"] && [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"host"]) + host = [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"host"]; + else + host = @""; + if([[spf objectForKey:@"data"] objectForKey:@"connection"] && [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"user"]) + user = [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"user"]; + else + user = @""; + if([[spf objectForKey:@"data"] objectForKey:@"connection"] && [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"database"]) + database = [[[spf objectForKey:@"data"] objectForKey:@"connection"] objectForKey:@"database"]; + else + database = @""; + } + + // compose the html + html = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:template, + [[iconImage TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.01] base64EncodingWithLineLength:0], + [spf objectForKey:@"rdbms_type"], + [spf objectForKey:@"rdbms_version"], + [name stringByReplacingOccurrencesOfString:@" " withString:@" "], + [host stringByReplacingOccurrencesOfString:@" " withString:@" "], + [user stringByReplacingOccurrencesOfString:@" " withString:@" "], + [database stringByReplacingOccurrencesOfString:@" " withString:@" "], + [NSString stringForByteSize:[[fileAttributes objectForKey:NSFileSize] longLongValue]], + [dateFormatter stringFromDate:[fileAttributes fileModificationDate]], + autoConnect + ]]; + + [dateFormatter release]; + [spf release]; + + } + else if([[spf objectForKey:@"format"] isEqualToString:@"content filters"]) { + template = [NSString stringWithContentsOfFile:[[NSBundle bundleWithIdentifier:@"com.google.code.sequel-pro.qlgenerator"] pathForResource:@"SPQLPluginContentFiltersTemplate" ofType:@"html"] + encoding:NSUTF8StringEncoding error:&templateReadError]; + + if (template == nil || ![template length] || templateReadError != nil) { + [pool release]; + return noErr; + } + // compose the html + html = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:template, + [[iconImage TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.01] base64EncodingWithLineLength:0], + [NSString stringWithContentsOfFile:[myURL path] encoding:NSUTF8StringEncoding error:nil] + ]]; + } + else if([[spf objectForKey:@"format"] isEqualToString:@"query favorites"]) { + template = [NSString stringWithContentsOfFile:[[NSBundle bundleWithIdentifier:@"com.google.code.sequel-pro.qlgenerator"] pathForResource:@"SPQLPluginQueryFavoritesTemplate" ofType:@"html"] + encoding:NSUTF8StringEncoding error:&templateReadError]; + + if (template == nil || ![template length] || templateReadError != nil) { + [pool release]; + return noErr; + } + // compose the html + html = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:template, + [[iconImage TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.01] base64EncodingWithLineLength:0], + [NSString stringWithContentsOfFile:[myURL path] encoding:NSUTF8StringEncoding error:nil] + ]]; + } + } + else if([urlExtension isEqualToString:@"sql"]) { + template = [NSString stringWithContentsOfFile:[[NSBundle bundleWithIdentifier:@"com.google.code.sequel-pro.qlgenerator"] pathForResource:@"SPQLPluginSQLTemplate" ofType:@"html"] + encoding:NSUTF8StringEncoding error:&templateReadError]; + + if (template == nil || ![template length] || templateReadError != nil) { + [pool release]; + return noErr; + } + + // compose the html + if(fileAttributes) + { + NSNumber *filesize = [fileAttributes objectForKey:NSFileSize]; + // catch large files since Finder blocks + if([filesize unsignedLongValue] > 6000000) { + html = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:template, + [[iconImage TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.01] base64EncodingWithLineLength:0], + [NSString stringForByteSize:[[fileAttributes objectForKey:NSFileSize] longLongValue]], + @"... SQL ..." + ]]; + } else { + html = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:template, + [[iconImage TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.01] base64EncodingWithLineLength:0], + [NSString stringForByteSize:[[fileAttributes objectForKey:NSFileSize] longLongValue]], + [NSString stringWithContentsOfFile:[myURL path] encoding:NSUTF8StringEncoding error:nil] + ]]; + } + } else { + html = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:template, + [[iconImage TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.01] base64EncodingWithLineLength:0], + [NSString stringForByteSize:[[fileAttributes objectForKey:NSFileSize] longLongValue]], + [NSString stringWithContentsOfFile:[myURL path] encoding:NSUTF8StringEncoding error:nil] + ]]; + } + } + + CFDictionaryRef properties = (CFDictionaryRef)[NSDictionary dictionary]; + QLPreviewRequestSetDataRepresentation(preview, + (CFDataRef)[html dataUsingEncoding:NSUTF8StringEncoding], + kUTTypeHTML, + properties + ); + [html release]; + + } + [pool release]; + return noErr; +} + +void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview) +{ + // implement only if supported +} diff --git a/Source/GenerateThumbnailForURL.m b/Source/GenerateThumbnailForURL.m new file mode 100644 index 00000000..cee74c74 --- /dev/null +++ b/Source/GenerateThumbnailForURL.m @@ -0,0 +1,86 @@ +// GenerateThumbnailForURL.m +// sequel-pro +// +// Created by Hans-Jörg Bibiko on Aug 04, 2010 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at <http://code.google.com/p/sequel-pro/> + +#include <CoreFoundation/CoreFoundation.h> +#include <CoreServices/CoreServices.h> +#include <QuickLook/QuickLook.h> +#import <Cocoa/Cocoa.h> + + +/* ----------------------------------------------------------------------------- +Generate a thumbnail for file + +This function's job is to create thumbnail for designated file as fast as possible +----------------------------------------------------------------------------- */ + + +void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail) +{ + // implement only if supported +} + +OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) + +{ + return noErr; + + // The following code is meant as example maybe fr the future + // NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + // + // NSData *thumbnailData = [NSData dataWithContentsOfFile:@"appicon.icns"]; + // if ( thumbnailData == nil || [thumbnailData length] == 0 ) { + // // Nothing Found. Don't care. + // [pool release]; + // return noErr; + // } + // + // NSSize canvasSize = NSMakeSize((NSInteger)(maxSize.height/1.3f), maxSize.height); + // + // // Thumbnail will be drawn with maximum resolution for desired thumbnail request + // // Here we create a graphics context to draw the Quick Look Thumbnail in. + // CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, *(CGSize *)&canvasSize, true, NULL); + // if(cgContext) { + // NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)cgContext flipped:YES]; + // if(context) { + // //These two lines of code are just good safe programming... + // [NSGraphicsContext saveGraphicsState]; + // [NSGraphicsContext setCurrentContext:context]; + // + // // [context setCompositingOperation:NSCompositeSourceOver]; + // // CGContextSetAlpha(cgContext, 0.5); + // + // NSBitmapImageRep *thumbnailBitmap = [NSBitmapImageRep imageRepWithData:thumbnailData]; + // [thumbnailBitmap drawInRect:NSMakeRect(10,10,200,200)]; + // + // //This line sets the context back to what it was when we're done + // [NSGraphicsContext restoreGraphicsState]; + // } + // + // // When we are done with our drawing code QLThumbnailRequestFlushContext() is called to flush the context + // QLThumbnailRequestFlushContext(thumbnail, cgContext); + // + // CFRelease(cgContext); + // } + // + // [pool release]; + // return noErr; + +}
\ No newline at end of file diff --git a/Source/main.c b/Source/main.c new file mode 100644 index 00000000..ae199979 --- /dev/null +++ b/Source/main.c @@ -0,0 +1,218 @@ +//============================================================================== +// +// DO NO MODIFY THE CONTENT OF THIS FILE +// +// This file contains the generic CFPlug-in code necessary for your generator +// To complete your generator implement the function in GenerateThumbnailForURL/GeneratePreviewForURL.c +// +//============================================================================== + + + + + + +#include <CoreFoundation/CoreFoundation.h> +#include <CoreFoundation/CFPlugInCOM.h> +#include <CoreServices/CoreServices.h> +#include <QuickLook/QuickLook.h> + +// ----------------------------------------------------------------------------- +// constants +// ----------------------------------------------------------------------------- + +// Don't modify this line +#define PLUGIN_ID "15621E48-2969-4291-807E-5F0498A9FB70" + +// +// Below is the generic glue code for all plug-ins. +// +// You should not have to modify this code aside from changing +// names if you decide to change the names defined in the Info.plist +// + + +// ----------------------------------------------------------------------------- +// typedefs +// ----------------------------------------------------------------------------- + +// The thumbnail generation function to be implemented in GenerateThumbnailForURL.c +OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize); +void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail); + +// The preview generation function to be implemented in GeneratePreviewForURL.c +OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options); +void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview); + +// The layout for an instance of QuickLookGeneratorPlugIn +typedef struct __QuickLookGeneratorPluginType +{ + void *conduitInterface; + CFUUIDRef factoryID; + UInt32 refCount; +} QuickLookGeneratorPluginType; + +// ----------------------------------------------------------------------------- +// prototypes +// ----------------------------------------------------------------------------- +// Forward declaration for the IUnknown implementation. +// + +QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID); +void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance); +HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv); +void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID); +ULONG QuickLookGeneratorPluginAddRef(void *thisInstance); +ULONG QuickLookGeneratorPluginRelease(void *thisInstance); + +// ----------------------------------------------------------------------------- +// myInterfaceFtbl definition +// ----------------------------------------------------------------------------- +// The QLGeneratorInterfaceStruct function table. +// +static QLGeneratorInterfaceStruct myInterfaceFtbl = { + NULL, + QuickLookGeneratorQueryInterface, + QuickLookGeneratorPluginAddRef, + QuickLookGeneratorPluginRelease, + NULL, + NULL, + NULL, + NULL +}; + + +// ----------------------------------------------------------------------------- +// AllocQuickLookGeneratorPluginType +// ----------------------------------------------------------------------------- +// Utility function that allocates a new instance. +// You can do some initial setup for the generator here if you wish +// like allocating globals etc... +// +QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID) +{ + QuickLookGeneratorPluginType *theNewInstance; + + theNewInstance = (QuickLookGeneratorPluginType *)malloc(sizeof(QuickLookGeneratorPluginType)); + memset(theNewInstance,0,sizeof(QuickLookGeneratorPluginType)); + + /* Point to the function table Malloc enough to store the stuff and copy the filler from myInterfaceFtbl over */ + theNewInstance->conduitInterface = malloc(sizeof(QLGeneratorInterfaceStruct)); + memcpy(theNewInstance->conduitInterface,&myInterfaceFtbl,sizeof(QLGeneratorInterfaceStruct)); + + /* Retain and keep an open instance refcount for each factory. */ + theNewInstance->factoryID = CFRetain(inFactoryID); + CFPlugInAddInstanceForFactory(inFactoryID); + + /* This function returns the IUnknown interface so set the refCount to one. */ + theNewInstance->refCount = 1; + return theNewInstance; +} + +// ----------------------------------------------------------------------------- +// DeallocQuickLookGeneratorPluginType +// ----------------------------------------------------------------------------- +// Utility function that deallocates the instance when +// the refCount goes to zero. +// In the current implementation generator interfaces are never deallocated +// but implement this as this might change in the future +// +void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance) +{ + CFUUIDRef theFactoryID; + + theFactoryID = thisInstance->factoryID; + /* Free the conduitInterface table up */ + free(thisInstance->conduitInterface); + + /* Free the instance structure */ + free(thisInstance); + if (theFactoryID){ + CFPlugInRemoveInstanceForFactory(theFactoryID); + CFRelease(theFactoryID); + } +} + +// ----------------------------------------------------------------------------- +// QuickLookGeneratorQueryInterface +// ----------------------------------------------------------------------------- +// Implementation of the IUnknown QueryInterface function. +// +HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv) +{ + CFUUIDRef interfaceID; + + interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid); + + if (CFEqual(interfaceID,kQLGeneratorCallbacksInterfaceID)){ + /* If the Right interface was requested, bump the ref count, + * set the ppv parameter equal to the instance, and + * return good status. + */ + ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->GenerateThumbnailForURL = GenerateThumbnailForURL; + ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->CancelThumbnailGeneration = CancelThumbnailGeneration; + ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->GeneratePreviewForURL = GeneratePreviewForURL; + ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->CancelPreviewGeneration = CancelPreviewGeneration; + ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType*)thisInstance)->conduitInterface)->AddRef(thisInstance); + *ppv = thisInstance; + CFRelease(interfaceID); + return S_OK; + }else{ + /* Requested interface unknown, bail with error. */ + *ppv = NULL; + CFRelease(interfaceID); + return E_NOINTERFACE; + } +} + +// ----------------------------------------------------------------------------- +// QuickLookGeneratorPluginAddRef +// ----------------------------------------------------------------------------- +// Implementation of reference counting for this type. Whenever an interface +// is requested, bump the refCount for the instance. NOTE: returning the +// refcount is a convention but is not required so don't rely on it. +// +ULONG QuickLookGeneratorPluginAddRef(void *thisInstance) +{ + ((QuickLookGeneratorPluginType *)thisInstance )->refCount += 1; + return ((QuickLookGeneratorPluginType*) thisInstance)->refCount; +} + +// ----------------------------------------------------------------------------- +// QuickLookGeneratorPluginRelease +// ----------------------------------------------------------------------------- +// When an interface is released, decrement the refCount. +// If the refCount goes to zero, deallocate the instance. +// +ULONG QuickLookGeneratorPluginRelease(void *thisInstance) +{ + ((QuickLookGeneratorPluginType*)thisInstance)->refCount -= 1; + if (((QuickLookGeneratorPluginType*)thisInstance)->refCount == 0){ + DeallocQuickLookGeneratorPluginType((QuickLookGeneratorPluginType*)thisInstance ); + return 0; + }else{ + return ((QuickLookGeneratorPluginType*) thisInstance )->refCount; + } +} + +// ----------------------------------------------------------------------------- +// QuickLookGeneratorPluginFactory +// ----------------------------------------------------------------------------- +void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID) +{ + QuickLookGeneratorPluginType *result; + CFUUIDRef uuid; + + /* If correct type is being requested, allocate an + * instance of kQLGeneratorTypeID and return the IUnknown interface. + */ + if (CFEqual(typeID,kQLGeneratorTypeID)){ + uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID)); + result = AllocQuickLookGeneratorPluginType(uuid); + CFRelease(uuid); + return result; + } + /* If the requested type is incorrect, return NULL. */ + return NULL; +} + |