/Importer/main.c

http://github.com/bububa/MongoHub-Mac · C · 224 lines · 97 code · 37 blank · 90 comment · 6 complexity · afc7aa4b9d017c93860105f1088aa651 MD5 · raw file

  1. //
  2. // main.c
  3. // MongoHub Spotlight Importer
  4. //
  5. // Created by Syd on 10-4-24.
  6. // Copyright (c) 2010 MusicPeace.ORG. All rights reserved.
  7. //
  8. //==============================================================================
  9. //
  10. // DO NO MODIFY THE CONTENT OF THIS FILE
  11. //
  12. // This file contains the generic CFPlug-in code necessary for your importer
  13. // To complete your importer implement the function in GetMetadataForFile.c
  14. //
  15. //==============================================================================
  16. #import <CoreFoundation/CoreFoundation.h>
  17. #import <CoreFoundation/CFPlugInCOM.h>
  18. #import <CoreServices/CoreServices.h>
  19. // -----------------------------------------------------------------------------
  20. // constants
  21. // -----------------------------------------------------------------------------
  22. #define PLUGIN_ID "E0C38F20-64C0-4B5E-98E5-834069A86AD4"
  23. //
  24. // Below is the generic glue code for all plug-ins.
  25. //
  26. // You should not have to modify this code aside from changing
  27. // names if you decide to change the names defined in the Info.plist
  28. //
  29. // -----------------------------------------------------------------------------
  30. // typedefs
  31. // -----------------------------------------------------------------------------
  32. // The import function to be implemented in GetMetadataForFile.c
  33. Boolean GetMetadataForFile(void *thisInterface,
  34. CFMutableDictionaryRef attributes,
  35. CFStringRef contentTypeUTI,
  36. CFStringRef pathToFile);
  37. // The layout for an instance of MetaDataImporterPlugIn
  38. typedef struct __MetadataImporterPluginType
  39. {
  40. MDImporterInterfaceStruct *conduitInterface;
  41. CFUUIDRef factoryID;
  42. UInt32 refCount;
  43. } MetadataImporterPluginType;
  44. // -----------------------------------------------------------------------------
  45. // prototypes
  46. // -----------------------------------------------------------------------------
  47. // Forward declaration for the IUnknown implementation.
  48. //
  49. MetadataImporterPluginType *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID);
  50. void DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance);
  51. HRESULT MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv);
  52. void *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID);
  53. ULONG MetadataImporterPluginAddRef(void *thisInstance);
  54. ULONG MetadataImporterPluginRelease(void *thisInstance);
  55. // -----------------------------------------------------------------------------
  56. // testInterfaceFtbl definition
  57. // -----------------------------------------------------------------------------
  58. // The TestInterface function table.
  59. //
  60. static MDImporterInterfaceStruct testInterfaceFtbl = {
  61. NULL,
  62. MetadataImporterQueryInterface,
  63. MetadataImporterPluginAddRef,
  64. MetadataImporterPluginRelease,
  65. GetMetadataForFile
  66. };
  67. // -----------------------------------------------------------------------------
  68. // AllocMetadataImporterPluginType
  69. // -----------------------------------------------------------------------------
  70. // Utility function that allocates a new instance.
  71. // You can do some initial setup for the importer here if you wish
  72. // like allocating globals etc...
  73. //
  74. MetadataImporterPluginType *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID)
  75. {
  76. MetadataImporterPluginType *theNewInstance;
  77. theNewInstance = (MetadataImporterPluginType *)malloc(sizeof(MetadataImporterPluginType));
  78. memset(theNewInstance,0,sizeof(MetadataImporterPluginType));
  79. /* Point to the function table */
  80. theNewInstance->conduitInterface = &testInterfaceFtbl;
  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. // DeallocMongoHubMDImporterPluginType
  90. // -----------------------------------------------------------------------------
  91. // Utility function that deallocates the instance when
  92. // the refCount goes to zero.
  93. // In the current implementation importer interfaces are never deallocated
  94. // but implement this as this might change in the future
  95. //
  96. void DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance)
  97. {
  98. CFUUIDRef theFactoryID;
  99. theFactoryID = thisInstance->factoryID;
  100. free(thisInstance);
  101. if (theFactoryID){
  102. CFPlugInRemoveInstanceForFactory(theFactoryID);
  103. CFRelease(theFactoryID);
  104. }
  105. }
  106. // -----------------------------------------------------------------------------
  107. // MetadataImporterQueryInterface
  108. // -----------------------------------------------------------------------------
  109. // Implementation of the IUnknown QueryInterface function.
  110. //
  111. HRESULT MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv)
  112. {
  113. CFUUIDRef interfaceID;
  114. interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid);
  115. if (CFEqual(interfaceID,kMDImporterInterfaceID)){
  116. /* If the Right interface was requested, bump the ref count,
  117. * set the ppv parameter equal to the instance, and
  118. * return good status.
  119. */
  120. ((MetadataImporterPluginType*)thisInstance)->conduitInterface->AddRef(thisInstance);
  121. *ppv = thisInstance;
  122. CFRelease(interfaceID);
  123. return S_OK;
  124. }else{
  125. if (CFEqual(interfaceID,IUnknownUUID)){
  126. /* If the IUnknown interface was requested, same as above. */
  127. ((MetadataImporterPluginType*)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. // -----------------------------------------------------------------------------
  140. // MetadataImporterPluginAddRef
  141. // -----------------------------------------------------------------------------
  142. // Implementation of reference counting for this type. Whenever an interface
  143. // is requested, bump the refCount for the instance. NOTE: returning the
  144. // refcount is a convention but is not required so don't rely on it.
  145. //
  146. ULONG MetadataImporterPluginAddRef(void *thisInstance)
  147. {
  148. ((MetadataImporterPluginType *)thisInstance )->refCount += 1;
  149. return ((MetadataImporterPluginType*) thisInstance)->refCount;
  150. }
  151. // -----------------------------------------------------------------------------
  152. // SampleCMPluginRelease
  153. // -----------------------------------------------------------------------------
  154. // When an interface is released, decrement the refCount.
  155. // If the refCount goes to zero, deallocate the instance.
  156. //
  157. ULONG MetadataImporterPluginRelease(void *thisInstance)
  158. {
  159. ((MetadataImporterPluginType*)thisInstance)->refCount -= 1;
  160. if (((MetadataImporterPluginType*)thisInstance)->refCount == 0){
  161. DeallocMetadataImporterPluginType((MetadataImporterPluginType*)thisInstance );
  162. return 0;
  163. }else{
  164. return ((MetadataImporterPluginType*) thisInstance )->refCount;
  165. }
  166. }
  167. // -----------------------------------------------------------------------------
  168. // MongoHubMDImporterPluginFactory
  169. // -----------------------------------------------------------------------------
  170. // Implementation of the factory function for this type.
  171. //
  172. void *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID)
  173. {
  174. MetadataImporterPluginType *result;
  175. CFUUIDRef uuid;
  176. /* If correct type is being requested, allocate an
  177. * instance of TestType and return the IUnknown interface.
  178. */
  179. if (CFEqual(typeID,kMDImporterTypeID)){
  180. uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID));
  181. result = AllocMetadataImporterPluginType(uuid);
  182. CFRelease(uuid);
  183. return result;
  184. }
  185. /* If the requested type is incorrect, return NULL. */
  186. return NULL;
  187. }