PageRenderTime 33ms CodeModel.GetById 25ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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