merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / source / macosx / spotlight / main.m
blobb9afc65e0f2727bff75e6cf4c72a2d074a1446c3
1 //
2 //  main.c
3 //  SpotlightTester
4 //
5 //  Created by Florian Heckl on 10.07.07.
6 //  Copyright (c) 2007 __MyCompanyName__. All rights reserved.
7 //
13 //==============================================================================
15 //      DO NO MODIFY THE CONTENT OF THIS FILE
17 //      This file contains the generic CFPlug-in code necessary for your importer
18 //      To complete your importer implement the function in GetMetadataForFile.c
20 //==============================================================================
27 #include <CoreFoundation/CoreFoundation.h>
28 #include <CoreFoundation/CFPlugInCOM.h>
29 #include <CoreServices/CoreServices.h>
31 // -----------------------------------------------------------------------------
32 //      constants
33 // -----------------------------------------------------------------------------
36 #define PLUGIN_ID "A3FCC88D-B9A6-4364-8B93-92123C8A2D18"
39 // Below is the generic glue code for all plug-ins.
41 // You should not have to modify this code aside from changing
42 // names if you decide to change the names defined in the Info.plist
46 // -----------------------------------------------------------------------------
47 //      typedefs
48 // -----------------------------------------------------------------------------
50 // The import function to be implemented in GetMetadataForFile.c
51 Boolean GetMetadataForFile(void *thisInterface, 
52                            CFMutableDictionaryRef attributes, 
53                            CFStringRef contentTypeUTI,
54                            CFStringRef pathToFile);
55                            
56 // The layout for an instance of MetaDataImporterPlugIn 
57 typedef struct __MetadataImporterPluginType
59     MDImporterInterfaceStruct *conduitInterface;
60     CFUUIDRef                 factoryID;
61     UInt32                    refCount;
62 } MetadataImporterPluginType;
64 // -----------------------------------------------------------------------------
65 //      prototypes
66 // -----------------------------------------------------------------------------
67 //      Forward declaration for the IUnknown implementation.
70 MetadataImporterPluginType  *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID);
71 void                      DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance);
72 HRESULT                   MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv);
73 void                     *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID);
74 ULONG                     MetadataImporterPluginAddRef(void *thisInstance);
75 ULONG                     MetadataImporterPluginRelease(void *thisInstance);
76 // -----------------------------------------------------------------------------
77 //      testInterfaceFtbl       definition
78 // -----------------------------------------------------------------------------
79 //      The TestInterface function table.
82 static MDImporterInterfaceStruct testInterfaceFtbl = {
83     NULL,
84     MetadataImporterQueryInterface,
85     MetadataImporterPluginAddRef,
86     MetadataImporterPluginRelease,
87     GetMetadataForFile
91 // -----------------------------------------------------------------------------
92 //      AllocMetadataImporterPluginType
93 // -----------------------------------------------------------------------------
94 //      Utility function that allocates a new instance.
95 //      You can do some initial setup for the importer here if you wish
96 //      like allocating globals etc...
98 MetadataImporterPluginType *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID)
100     MetadataImporterPluginType *theNewInstance;
102     theNewInstance = (MetadataImporterPluginType *)malloc(sizeof(MetadataImporterPluginType));
103     memset(theNewInstance,0,sizeof(MetadataImporterPluginType));
105         /* Point to the function table */
106     theNewInstance->conduitInterface = &testInterfaceFtbl;
108         /*  Retain and keep an open instance refcount for each factory. */
109     theNewInstance->factoryID = CFRetain(inFactoryID);
110     CFPlugInAddInstanceForFactory(inFactoryID);
112         /* This function returns the IUnknown interface so set the refCount to one. */
113     theNewInstance->refCount = 1;
114     return theNewInstance;
117 // -----------------------------------------------------------------------------
118 //      DeallocSpotlightTesterMDImporterPluginType
119 // -----------------------------------------------------------------------------
120 //      Utility function that deallocates the instance when
121 //      the refCount goes to zero.
122 //      In the current implementation importer interfaces are never deallocated
123 //      but implement this as this might change in the future
125 void DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance)
127     CFUUIDRef theFactoryID;
129     theFactoryID = thisInstance->factoryID;
130     free(thisInstance);
131     if (theFactoryID){
132         CFPlugInRemoveInstanceForFactory(theFactoryID);
133         CFRelease(theFactoryID);
134     }
137 // -----------------------------------------------------------------------------
138 //      MetadataImporterQueryInterface
139 // -----------------------------------------------------------------------------
140 //      Implementation of the IUnknown QueryInterface function.
142 HRESULT MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv)
144     CFUUIDRef interfaceID;
146     interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid);
148     if (CFEqual(interfaceID,kMDImporterInterfaceID)){
149             /* If the Right interface was requested, bump the ref count,
150              * set the ppv parameter equal to the instance, and
151              * return good status.
152              */
153         ((MetadataImporterPluginType*)thisInstance)->conduitInterface->AddRef(thisInstance);
154         *ppv = thisInstance;
155         CFRelease(interfaceID);
156         return S_OK;
157     }else{
158         if (CFEqual(interfaceID,IUnknownUUID)){
159                 /* If the IUnknown interface was requested, same as above. */
160             ((MetadataImporterPluginType*)thisInstance )->conduitInterface->AddRef(thisInstance);
161             *ppv = thisInstance;
162             CFRelease(interfaceID);
163             return S_OK;
164         }else{
165                 /* Requested interface unknown, bail with error. */
166             *ppv = NULL;
167             CFRelease(interfaceID);
168             return E_NOINTERFACE;
169         }
170     }
173 // -----------------------------------------------------------------------------
174 //      MetadataImporterPluginAddRef
175 // -----------------------------------------------------------------------------
176 //      Implementation of reference counting for this type. Whenever an interface
177 //      is requested, bump the refCount for the instance. NOTE: returning the
178 //      refcount is a convention but is not required so don't rely on it.
180 ULONG MetadataImporterPluginAddRef(void *thisInstance)
182     ((MetadataImporterPluginType *)thisInstance )->refCount += 1;
183     return ((MetadataImporterPluginType*) thisInstance)->refCount;
186 // -----------------------------------------------------------------------------
187 // SampleCMPluginRelease
188 // -----------------------------------------------------------------------------
189 //      When an interface is released, decrement the refCount.
190 //      If the refCount goes to zero, deallocate the instance.
192 ULONG MetadataImporterPluginRelease(void *thisInstance)
194     ((MetadataImporterPluginType*)thisInstance)->refCount -= 1;
195     if (((MetadataImporterPluginType*)thisInstance)->refCount == 0){
196         DeallocMetadataImporterPluginType((MetadataImporterPluginType*)thisInstance );
197         return 0;
198     }else{
199         return ((MetadataImporterPluginType*) thisInstance )->refCount;
200     }
203 // -----------------------------------------------------------------------------
204 //      SpotlightTesterMDImporterPluginFactory
205 // -----------------------------------------------------------------------------
206 //      Implementation of the factory function for this type.
208 void *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID)
210     MetadataImporterPluginType *result;
211     CFUUIDRef                 uuid;
213         /* If correct type is being requested, allocate an
214          * instance of TestType and return the IUnknown interface.
215          */
216     if (CFEqual(typeID,kMDImporterTypeID)){
217         uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID));
218         result = AllocMetadataImporterPluginType(uuid);
219         CFRelease(uuid);
220         return result;
221     }
222         /* If the requested type is incorrect, return NULL. */
223     return NULL;