main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //==============================================================================
  2. //
  3. // DO NO MODIFY THE CONTENT OF THIS FILE
  4. //
  5. // This file contains the generic CFPlug-in code necessary for your generator
  6. // To complete your generator implement the function in GenerateThumbnailForURL/GeneratePreviewForURL.c
  7. //
  8. //==============================================================================
  9. #include <CoreFoundation/CoreFoundation.h>
  10. #include <CoreFoundation/CFPlugInCOM.h>
  11. #include <CoreServices/CoreServices.h>
  12. #include <QuickLook/QuickLook.h>
  13. // -----------------------------------------------------------------------------
  14. // constants
  15. // -----------------------------------------------------------------------------
  16. // Don't modify this line
  17. #define PLUGIN_ID "C00EB589-55DF-45ED-B4B8-8AFFA367CFCF"
  18. //
  19. // Below is the generic glue code for all plug-ins.
  20. //
  21. // You should not have to modify this code aside from changing
  22. // names if you decide to change the names defined in the Info.plist
  23. //
  24. // -----------------------------------------------------------------------------
  25. // typedefs
  26. // -----------------------------------------------------------------------------
  27. // The thumbnail generation function to be implemented in GenerateThumbnailForURL.c
  28. OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize);
  29. void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail);
  30. // The preview generation function to be implemented in GeneratePreviewForURL.c
  31. OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
  32. void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
  33. // The layout for an instance of QuickLookGeneratorPlugIn
  34. typedef struct __QuickLookGeneratorPluginType
  35. {
  36. void *conduitInterface;
  37. CFUUIDRef factoryID;
  38. UInt32 refCount;
  39. } QuickLookGeneratorPluginType;
  40. // -----------------------------------------------------------------------------
  41. // prototypes
  42. // -----------------------------------------------------------------------------
  43. // Forward declaration for the IUnknown implementation.
  44. //
  45. QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID);
  46. void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance);
  47. HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv);
  48. void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID);
  49. ULONG QuickLookGeneratorPluginAddRef(void *thisInstance);
  50. ULONG QuickLookGeneratorPluginRelease(void *thisInstance);
  51. // -----------------------------------------------------------------------------
  52. // myInterfaceFtbl definition
  53. // -----------------------------------------------------------------------------
  54. // The QLGeneratorInterfaceStruct function table.
  55. //
  56. static QLGeneratorInterfaceStruct myInterfaceFtbl = {
  57. NULL,
  58. QuickLookGeneratorQueryInterface,
  59. QuickLookGeneratorPluginAddRef,
  60. QuickLookGeneratorPluginRelease,
  61. NULL,
  62. NULL,
  63. NULL,
  64. NULL
  65. };
  66. // -----------------------------------------------------------------------------
  67. // AllocQuickLookGeneratorPluginType
  68. // -----------------------------------------------------------------------------
  69. // Utility function that allocates a new instance.
  70. // You can do some initial setup for the generator here if you wish
  71. // like allocating globals etc...
  72. //
  73. QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFactoryID)
  74. {
  75. QuickLookGeneratorPluginType *theNewInstance;
  76. theNewInstance = (QuickLookGeneratorPluginType *)malloc(sizeof(QuickLookGeneratorPluginType));
  77. memset(theNewInstance,0,sizeof(QuickLookGeneratorPluginType));
  78. /* Point to the function table Malloc enough to store the stuff and copy the filler from myInterfaceFtbl over */
  79. theNewInstance->conduitInterface = malloc(sizeof(QLGeneratorInterfaceStruct));
  80. memcpy(theNewInstance->conduitInterface,&myInterfaceFtbl,sizeof(QLGeneratorInterfaceStruct));
  81. /* Retain and keep an open instance refcount for each factory. */
  82. theNewInstance->factoryID = CFRetain(inFactoryID);
  83. CFPlugInAddInstanceForFactory(inFactoryID);
  84. /* This function returns the IUnknown interface so set the refCount to one. */
  85. theNewInstance->refCount = 1;
  86. return theNewInstance;
  87. }
  88. // -----------------------------------------------------------------------------
  89. // DeallocQuickLookGeneratorPluginType
  90. // -----------------------------------------------------------------------------
  91. // Utility function that deallocates the instance when
  92. // the refCount goes to zero.
  93. // In the current implementation generator interfaces are never deallocated
  94. // but implement this as this might change in the future
  95. //
  96. void DeallocQuickLookGeneratorPluginType(QuickLookGeneratorPluginType *thisInstance)
  97. {
  98. CFUUIDRef theFactoryID;
  99. theFactoryID = thisInstance->factoryID;
  100. /* Free the conduitInterface table up */
  101. free(thisInstance->conduitInterface);
  102. /* Free the instance structure */
  103. free(thisInstance);
  104. if (theFactoryID){
  105. CFPlugInRemoveInstanceForFactory(theFactoryID);
  106. CFRelease(theFactoryID);
  107. }
  108. }
  109. // -----------------------------------------------------------------------------
  110. // QuickLookGeneratorQueryInterface
  111. // -----------------------------------------------------------------------------
  112. // Implementation of the IUnknown QueryInterface function.
  113. //
  114. HRESULT QuickLookGeneratorQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv)
  115. {
  116. CFUUIDRef interfaceID;
  117. interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid);
  118. if (CFEqual(interfaceID,kQLGeneratorCallbacksInterfaceID)){
  119. /* If the Right interface was requested, bump the ref count,
  120. * set the ppv parameter equal to the instance, and
  121. * return good status.
  122. */
  123. ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->GenerateThumbnailForURL = GenerateThumbnailForURL;
  124. ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->CancelThumbnailGeneration = CancelThumbnailGeneration;
  125. ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->GeneratePreviewForURL = GeneratePreviewForURL;
  126. ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType *)thisInstance)->conduitInterface)->CancelPreviewGeneration = CancelPreviewGeneration;
  127. ((QLGeneratorInterfaceStruct *)((QuickLookGeneratorPluginType*)thisInstance)->conduitInterface)->AddRef(thisInstance);
  128. *ppv = thisInstance;
  129. CFRelease(interfaceID);
  130. return S_OK;
  131. }else{
  132. /* Requested interface unknown, bail with error. */
  133. *ppv = NULL;
  134. CFRelease(interfaceID);
  135. return E_NOINTERFACE;
  136. }
  137. }
  138. // -----------------------------------------------------------------------------
  139. // QuickLookGeneratorPluginAddRef
  140. // -----------------------------------------------------------------------------
  141. // Implementation of reference counting for this type. Whenever an interface
  142. // is requested, bump the refCount for the instance. NOTE: returning the
  143. // refcount is a convention but is not required so don't rely on it.
  144. //
  145. ULONG QuickLookGeneratorPluginAddRef(void *thisInstance)
  146. {
  147. ((QuickLookGeneratorPluginType *)thisInstance )->refCount += 1;
  148. return ((QuickLookGeneratorPluginType*) thisInstance)->refCount;
  149. }
  150. // -----------------------------------------------------------------------------
  151. // QuickLookGeneratorPluginRelease
  152. // -----------------------------------------------------------------------------
  153. // When an interface is released, decrement the refCount.
  154. // If the refCount goes to zero, deallocate the instance.
  155. //
  156. ULONG QuickLookGeneratorPluginRelease(void *thisInstance)
  157. {
  158. ((QuickLookGeneratorPluginType*)thisInstance)->refCount -= 1;
  159. if (((QuickLookGeneratorPluginType*)thisInstance)->refCount == 0){
  160. DeallocQuickLookGeneratorPluginType((QuickLookGeneratorPluginType*)thisInstance );
  161. return 0;
  162. }else{
  163. return ((QuickLookGeneratorPluginType*) thisInstance )->refCount;
  164. }
  165. }
  166. // -----------------------------------------------------------------------------
  167. // QuickLookGeneratorPluginFactory
  168. // -----------------------------------------------------------------------------
  169. void *QuickLookGeneratorPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID)
  170. {
  171. QuickLookGeneratorPluginType *result;
  172. CFUUIDRef uuid;
  173. /* If correct type is being requested, allocate an
  174. * instance of kQLGeneratorTypeID and return the IUnknown interface.
  175. */
  176. if (CFEqual(typeID,kQLGeneratorTypeID)){
  177. uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID));
  178. result = AllocQuickLookGeneratorPluginType(uuid);
  179. CFRelease(uuid);
  180. return result;
  181. }
  182. /* If the requested type is incorrect, return NULL. */
  183. return NULL;
  184. }