main.c

Go to the documentation of this file.
00001 //==============================================================================
00002 //
00003 //      DO NO MODIFY THE CONTENT OF THIS FILE
00004 //
00005 //      This file contains the generic CFPlug-in code necessary for your importer
00006 //      To complete your importer implement the function in GetMetadataForFile.c
00007 //
00008 //==============================================================================
00009 
00010 
00011 
00012 
00013 
00014 
00015 #include <CoreFoundation/CoreFoundation.h>
00016 #include <CoreFoundation/CFPlugInCOM.h>
00017 #include <CoreServices/CoreServices.h>
00018 
00019 // -----------------------------------------------------------------------------
00020 //      constants
00021 // -----------------------------------------------------------------------------
00022 
00023 
00024 #define PLUGIN_ID "57D03001-2009-43F0-BB98-1BECC352D3AC"
00025 
00026 //
00027 // Below is the generic glue code for all plug-ins.
00028 //
00029 // You should not have to modify this code aside from changing
00030 // names if you decide to change the names defined in the Info.plist
00031 //
00032 
00033 
00034 // -----------------------------------------------------------------------------
00035 //      typedefs
00036 // -----------------------------------------------------------------------------
00037 
00038 // The import function to be implemented in GetMetadataForFile.c
00039 Boolean GetMetadataForFile(void *thisInterface, 
00040                            CFMutableDictionaryRef attributes, 
00041                            CFStringRef contentTypeUTI,
00042                            CFStringRef pathToFile);
00043                            
00044 // The layout for an instance of MetaDataImporterPlugIn 
00045 typedef struct __MetadataImporterPluginType
00046 {
00047     MDImporterInterfaceStruct *conduitInterface;
00048     CFUUIDRef                 factoryID;
00049     UInt32                    refCount;
00050 } MetadataImporterPluginType;
00051 
00052 // -----------------------------------------------------------------------------
00053 //      prototypes
00054 // -----------------------------------------------------------------------------
00055 //      Forward declaration for the IUnknown implementation.
00056 //
00057 
00058 MetadataImporterPluginType  *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID);
00059 void                      DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance);
00060 HRESULT                   MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv);
00061 void                     *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID);
00062 ULONG                     MetadataImporterPluginAddRef(void *thisInstance);
00063 ULONG                     MetadataImporterPluginRelease(void *thisInstance);
00064 // -----------------------------------------------------------------------------
00065 //      testInterfaceFtbl       definition
00066 // -----------------------------------------------------------------------------
00067 //      The TestInterface function table.
00068 //
00069 
00070 static MDImporterInterfaceStruct testInterfaceFtbl = {
00071     NULL,
00072     MetadataImporterQueryInterface,
00073     MetadataImporterPluginAddRef,
00074     MetadataImporterPluginRelease,
00075     GetMetadataForFile
00076 };
00077 
00078 
00079 // -----------------------------------------------------------------------------
00080 //      AllocMetadataImporterPluginType
00081 // -----------------------------------------------------------------------------
00082 //      Utility function that allocates a new instance.
00083 //      You can do some initial setup for the importer here if you wish
00084 //      like allocating globals etc...
00085 //
00086 MetadataImporterPluginType *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID)
00087 {
00088     MetadataImporterPluginType *theNewInstance;
00089 
00090     theNewInstance = (MetadataImporterPluginType *)malloc(sizeof(MetadataImporterPluginType));
00091     memset(theNewInstance,0,sizeof(MetadataImporterPluginType));
00092 
00093         /* Point to the function table */
00094     theNewInstance->conduitInterface = &testInterfaceFtbl;
00095 
00096         /*  Retain and keep an open instance refcount for each factory. */
00097     theNewInstance->factoryID = CFRetain(inFactoryID);
00098     CFPlugInAddInstanceForFactory(inFactoryID);
00099 
00100         /* This function returns the IUnknown interface so set the refCount to one. */
00101     theNewInstance->refCount = 1;
00102     return theNewInstance;
00103 }
00104 
00105 // -----------------------------------------------------------------------------
00106 //      DeallocROOTSLMDImporterPluginType
00107 // -----------------------------------------------------------------------------
00108 //      Utility function that deallocates the instance when
00109 //      the refCount goes to zero.
00110 //      In the current implementation importer interfaces are never deallocated
00111 //      but implement this as this might change in the future
00112 //
00113 void DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance)
00114 {
00115     CFUUIDRef theFactoryID;
00116 
00117     theFactoryID = thisInstance->factoryID;
00118     free(thisInstance);
00119     if (theFactoryID){
00120         CFPlugInRemoveInstanceForFactory(theFactoryID);
00121         CFRelease(theFactoryID);
00122     }
00123 }
00124 
00125 // -----------------------------------------------------------------------------
00126 //      MetadataImporterQueryInterface
00127 // -----------------------------------------------------------------------------
00128 //      Implementation of the IUnknown QueryInterface function.
00129 //
00130 HRESULT MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv)
00131 {
00132     CFUUIDRef interfaceID;
00133 
00134     interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid);
00135 
00136     if (CFEqual(interfaceID,kMDImporterInterfaceID)){
00137             /* If the Right interface was requested, bump the ref count,
00138              * set the ppv parameter equal to the instance, and
00139              * return good status.
00140              */
00141         ((MetadataImporterPluginType*)thisInstance)->conduitInterface->AddRef(thisInstance);
00142         *ppv = thisInstance;
00143         CFRelease(interfaceID);
00144         return S_OK;
00145     }else{
00146         if (CFEqual(interfaceID,IUnknownUUID)){
00147                 /* If the IUnknown interface was requested, same as above. */
00148             ((MetadataImporterPluginType*)thisInstance )->conduitInterface->AddRef(thisInstance);
00149             *ppv = thisInstance;
00150             CFRelease(interfaceID);
00151             return S_OK;
00152         }else{
00153                 /* Requested interface unknown, bail with error. */
00154             *ppv = NULL;
00155             CFRelease(interfaceID);
00156             return E_NOINTERFACE;
00157         }
00158     }
00159 }
00160 
00161 // -----------------------------------------------------------------------------
00162 //      MetadataImporterPluginAddRef
00163 // -----------------------------------------------------------------------------
00164 //      Implementation of reference counting for this type. Whenever an interface
00165 //      is requested, bump the refCount for the instance. NOTE: returning the
00166 //      refcount is a convention but is not required so don't rely on it.
00167 //
00168 ULONG MetadataImporterPluginAddRef(void *thisInstance)
00169 {
00170     ((MetadataImporterPluginType *)thisInstance )->refCount += 1;
00171     return ((MetadataImporterPluginType*) thisInstance)->refCount;
00172 }
00173 
00174 // -----------------------------------------------------------------------------
00175 // SampleCMPluginRelease
00176 // -----------------------------------------------------------------------------
00177 //      When an interface is released, decrement the refCount.
00178 //      If the refCount goes to zero, deallocate the instance.
00179 //
00180 ULONG MetadataImporterPluginRelease(void *thisInstance)
00181 {
00182     ((MetadataImporterPluginType*)thisInstance)->refCount -= 1;
00183     if (((MetadataImporterPluginType*)thisInstance)->refCount == 0){
00184         DeallocMetadataImporterPluginType((MetadataImporterPluginType*)thisInstance );
00185         return 0;
00186     }else{
00187         return ((MetadataImporterPluginType*) thisInstance )->refCount;
00188     }
00189 }
00190 
00191 // -----------------------------------------------------------------------------
00192 //      ROOTSLMDImporterPluginFactory
00193 // -----------------------------------------------------------------------------
00194 //      Implementation of the factory function for this type.
00195 //
00196 void *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID)
00197 {
00198     MetadataImporterPluginType *result;
00199     CFUUIDRef                 uuid;
00200 
00201         /* If correct type is being requested, allocate an
00202          * instance of TestType and return the IUnknown interface.
00203          */
00204     if (CFEqual(typeID,kMDImporterTypeID)){
00205         uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID));
00206         result = AllocMetadataImporterPluginType(uuid);
00207         CFRelease(uuid);
00208         return result;
00209     }
00210         /* If the requested type is incorrect, return NULL. */
00211     return NULL;
00212 }
00213 

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