root/trunk/Importer/main.c

Revision 3418, 4.4 kB (checked in by timothy, 2 years ago)

Turing on more warnings and fixing more of them.

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