root/tags/2D14/Importer/main.c

Revision 2584, 4.4 kB (checked in by timothy, 4 years ago)

Should be properly testing if we are building on 10.4 now.
Added an autorelease pool, the docs said not to assume.

Line 
1 #include <AvailabilityMacros.h>
2
3 #ifdef MAC_OS_X_VERSION_10_4
4
5 #include <CoreFoundation/CoreFoundation.h>
6 #include <CoreFoundation/CFPlugInCOM.h>
7 #include <CoreServices/CoreServices.h>
8
9 #define PLUGIN_ID "04A856E0-880E-41BA-ABFA-35F147710AFC"
10
11 // The import function to be implemented in GetMetadataForFile.c
12 Boolean GetMetadataForFile(void *thisInterface,
13                            CFMutableDictionaryRef attributes,
14                            CFStringRef contentTypeUTI,
15                            CFStringRef pathToFile);
16                            
17 // The layout for an instance of MetaDataImporterPlugIn
18 typedef struct __MetadataImporterPluginType
19 {
20         MDImporterInterfaceStruct *conduitInterface;
21         CFUUIDRef                 factoryID;
22         UInt32                    refCount;
23 } MetadataImporterPluginType;
24
25
26 MetadataImporterPluginType  *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID);
27 void                      DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance);
28 HRESULT                   MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv);
29 void                     *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID);
30 ULONG                     MetadataImporterPluginAddRef(void *thisInstance);
31 ULONG                     MetadataImporterPluginRelease(void *thisInstance);
32
33 static MDImporterInterfaceStruct testInterfaceFtbl = {
34         NULL,
35         MetadataImporterQueryInterface,
36         MetadataImporterPluginAddRef,
37         MetadataImporterPluginRelease,
38         GetMetadataForFile
39 };
40
41 MetadataImporterPluginType *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID)
42 {
43         MetadataImporterPluginType *theNewInstance;
44
45         theNewInstance = (MetadataImporterPluginType *)malloc(sizeof(MetadataImporterPluginType));
46         memset(theNewInstance,0,sizeof(MetadataImporterPluginType));
47
48                 /* Point to the function table */
49         theNewInstance->conduitInterface = &testInterfaceFtbl;
50
51                 /*  Retain and keep an open instance refcount for each factory. */
52         theNewInstance->factoryID = CFRetain(inFactoryID);
53         CFPlugInAddInstanceForFactory(inFactoryID);
54
55                 /* This function returns the IUnknown interface so set the refCount to one. */
56         theNewInstance->refCount = 1;
57         return theNewInstance;
58 }
59
60 void DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance)
61 {
62         CFUUIDRef theFactoryID;
63
64         theFactoryID = thisInstance->factoryID;
65         free(thisInstance);
66         if (theFactoryID){
67                 CFPlugInRemoveInstanceForFactory(theFactoryID);
68                 CFRelease(theFactoryID);
69         }
70 }
71
72 HRESULT MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv)
73 {
74         CFUUIDRef interfaceID;
75
76         interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid);
77
78         if (CFEqual(interfaceID,kMDImporterInterfaceID)){
79                         /* If the Right interface was requested, bump the ref count,
80                          * set the ppv parameter equal to the instance, and
81                          * return good status.
82                          */
83                 ((MetadataImporterPluginType*)thisInstance)->conduitInterface->AddRef(thisInstance);
84                 *ppv = thisInstance;
85                 CFRelease(interfaceID);
86                 return S_OK;
87         }else{
88                 if (CFEqual(interfaceID,IUnknownUUID)){
89                                 /* If the IUnknown interface was requested, same as above. */
90                         ((MetadataImporterPluginType*)thisInstance )->conduitInterface->AddRef(thisInstance);
91                         *ppv = thisInstance;
92                         CFRelease(interfaceID);
93                         return S_OK;
94                 }else{
95                                 /* Requested interface unknown, bail with error. */
96                         *ppv = NULL;
97                         CFRelease(interfaceID);
98                         return E_NOINTERFACE;
99                 }
100         }
101 }
102
103 ULONG MetadataImporterPluginAddRef(void *thisInstance)
104 {
105         ((MetadataImporterPluginType *)thisInstance )->refCount += 1;
106         return ((MetadataImporterPluginType*) thisInstance)->refCount;
107 }
108
109 ULONG MetadataImporterPluginRelease(void *thisInstance)
110 {
111         ((MetadataImporterPluginType*)thisInstance)->refCount -= 1;
112         if (((MetadataImporterPluginType*)thisInstance)->refCount == 0){
113                 DeallocMetadataImporterPluginType((MetadataImporterPluginType*)thisInstance );
114                 return 0;
115         }else{
116                 return ((MetadataImporterPluginType*) thisInstance )->refCount;
117         }
118 }
119
120 void *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID)
121 {
122         MetadataImporterPluginType *result;
123         CFUUIDRef                 uuid;
124
125                 /* If correct type is being requested, allocate an
126                  * instance of TestType and return the IUnknown interface.
127                  */
128         if (CFEqual(typeID,kMDImporterTypeID)){
129                 uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID));
130                 result = AllocMetadataImporterPluginType(uuid);
131                 CFRelease(uuid);
132                 return result;
133         }
134                 /* If the requested type is incorrect, return NULL. */
135         return NULL;
136 }
137
138 #endif
Note: See TracBrowser for help on using the browser.