Back to index

lightning-sunbird  0.9+nobinonly
main.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004 *
00005 * The contents of this file are subject to the Mozilla Public License Version
00006 * 1.1 (the "License"); you may not use this file except in compliance with
00007 * the License. You may obtain a copy of the License at
00008 * http://www.mozilla.org/MPL/
00009 *
00010 * Software distributed under the License is distributed on an "AS IS" basis,
00011 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012 * for the specific language governing rights and limitations under the
00013 * License.
00014 *
00015 * The Original Code is mozilla.org code.
00016 *
00017 * The Initial Developer of the Original Code is
00018 * Netscape Communications Corporation.
00019 * Portions created by the Initial Developer are Copyright (C) 2007
00020 * the Initial Developer. All Rights Reserved.
00021 *
00022 * Contributor(s):
00023 *   David Bienvenu <bienvenu@nventure.com>
00024 *
00025 * Alternatively, the contents of this file may be used under the terms of
00026 * either of the GNU General Public License Version 2 or later (the "GPL"),
00027 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028 * in which case the provisions of the GPL or the LGPL are applicable instead
00029 * of those above. If you wish to allow use of your version of this file only
00030 * under the terms of either the GPL or the LGPL, and not to allow others to
00031 * use your version of this file under the terms of the MPL, indicate your
00032 * decision by deleting the provisions above and replace them with the notice
00033 * and other provisions required by the GPL or the LGPL. If you do not delete
00034 * the provisions above, a recipient may use your version of this file under
00035 * the terms of any one of the MPL, the GPL or the LGPL.
00036 *
00037 * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 
00041 //==============================================================================
00042 //
00043 //     DO NO MODIFY THE CONTENT OF THIS FILE
00044 //
00045 //     This file contains the generic CFPlug-in code necessary for TB Spotlight
00046 //     The actual importer is implemented in GetMetadataForFile.c
00047 //
00048 //==============================================================================
00049 
00050 
00051 
00052 #include <CoreFoundation/CoreFoundation.h>
00053 #include <CoreFoundation/CFPlugInCOM.h>
00054 #include <CoreServices/CoreServices.h>
00055 
00056 // -----------------------------------------------------------------------------
00057 //     constants
00058 // -----------------------------------------------------------------------------
00059 
00060 
00061 #define PLUGIN_ID "37401ADE-1058-42DB-BBE5-F2AAB9D7C13E"
00062 
00063 //
00064 // Below is the generic glue code for all plug-ins.
00065 //
00066 // You should not have to modify this code aside from changing
00067 // names if you decide to change the names defined in the Info.plist
00068 //
00069 
00070 
00071 // -----------------------------------------------------------------------------
00072 //     typedefs
00073 // -----------------------------------------------------------------------------
00074 
00075 // The import function to be implemented in GetMetadataForFile.c
00076 Boolean GetMetadataForFile(void *thisInterface, 
00077                         CFMutableDictionaryRef attributes, 
00078                         CFStringRef contentTypeUTI,
00079                         CFStringRef pathToFile);
00080                         
00081 // The layout for an instance of MetaDataImporterPlugIn 
00082 typedef struct __MetadataImporterPluginType
00083 {
00084     MDImporterInterfaceStruct *conduitInterface;
00085     CFUUIDRef                 factoryID;
00086     UInt32                    refCount;
00087 } MetadataImporterPluginType;
00088 
00089 // -----------------------------------------------------------------------------
00090 //     prototypes
00091 // -----------------------------------------------------------------------------
00092 //     Forward declaration for the IUnknown implementation.
00093 //
00094 
00095 MetadataImporterPluginType  *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID);
00096 void                      DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance);
00097 HRESULT                   MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv);
00098 void                     *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID);
00099 ULONG                     MetadataImporterPluginAddRef(void *thisInstance);
00100 ULONG                     MetadataImporterPluginRelease(void *thisInstance);
00101 // -----------------------------------------------------------------------------
00102 //     testInterfaceFtbl    definition
00103 // -----------------------------------------------------------------------------
00104 //     The TestInterface function table.
00105 //
00106 
00107 static MDImporterInterfaceStruct testInterfaceFtbl = {
00108     NULL,
00109     MetadataImporterQueryInterface,
00110     MetadataImporterPluginAddRef,
00111     MetadataImporterPluginRelease,
00112     GetMetadataForFile
00113 };
00114 
00115 
00116 // -----------------------------------------------------------------------------
00117 //     AllocMetadataImporterPluginType
00118 // -----------------------------------------------------------------------------
00119 //     Utility function that allocates a new instance.
00120 //      You can do some initial setup for the importer here if you wish
00121 //      like allocating globals etc...
00122 //
00123 MetadataImporterPluginType *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID)
00124 {
00125     MetadataImporterPluginType *theNewInstance;
00126 
00127     theNewInstance = (MetadataImporterPluginType *)malloc(sizeof(MetadataImporterPluginType));
00128     memset(theNewInstance,0,sizeof(MetadataImporterPluginType));
00129 
00130         /* Point to the function table */
00131     theNewInstance->conduitInterface = &testInterfaceFtbl;
00132 
00133         /*  Retain and keep an open instance refcount for each factory. */
00134     theNewInstance->factoryID = CFRetain(inFactoryID);
00135     CFPlugInAddInstanceForFactory(inFactoryID);
00136 
00137         /* This function returns the IUnknown interface so set the refCount to one. */
00138     theNewInstance->refCount = 1;
00139     return theNewInstance;
00140 }
00141 
00142 // -----------------------------------------------------------------------------
00143 //     DeallocTBSpotlightMDImporterPluginType
00144 // -----------------------------------------------------------------------------
00145 //     Utility function that deallocates the instance when
00146 //     the refCount goes to zero.
00147 //      In the current implementation importer interfaces are never deallocated
00148 //      but implement this as this might change in the future
00149 //
00150 void DeallocMetadataImporterPluginType(MetadataImporterPluginType *thisInstance)
00151 {
00152     CFUUIDRef theFactoryID;
00153 
00154     theFactoryID = thisInstance->factoryID;
00155     free(thisInstance);
00156     if (theFactoryID){
00157         CFPlugInRemoveInstanceForFactory(theFactoryID);
00158         CFRelease(theFactoryID);
00159     }
00160 }
00161 
00162 // -----------------------------------------------------------------------------
00163 //     MetadataImporterQueryInterface
00164 // -----------------------------------------------------------------------------
00165 //     Implementation of the IUnknown QueryInterface function.
00166 //
00167 HRESULT MetadataImporterQueryInterface(void *thisInstance,REFIID iid,LPVOID *ppv)
00168 {
00169     CFUUIDRef interfaceID;
00170 
00171     interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault,iid);
00172 
00173     if (CFEqual(interfaceID,kMDImporterInterfaceID)){
00174             /* If the Right interface was requested, bump the ref count,
00175              * set the ppv parameter equal to the instance, and
00176              * return good status.
00177              */
00178         ((MetadataImporterPluginType*)thisInstance)->conduitInterface->AddRef(thisInstance);
00179         *ppv = thisInstance;
00180         CFRelease(interfaceID);
00181         return S_OK;
00182     }else{
00183         if (CFEqual(interfaceID,IUnknownUUID)){
00184                 /* If the IUnknown interface was requested, same as above. */
00185             ((MetadataImporterPluginType*)thisInstance )->conduitInterface->AddRef(thisInstance);
00186             *ppv = thisInstance;
00187             CFRelease(interfaceID);
00188             return S_OK;
00189         }else{
00190                 /* Requested interface unknown, bail with error. */
00191             *ppv = NULL;
00192             CFRelease(interfaceID);
00193             return E_NOINTERFACE;
00194         }
00195     }
00196 }
00197 
00198 // -----------------------------------------------------------------------------
00199 //     MetadataImporterPluginAddRef
00200 // -----------------------------------------------------------------------------
00201 //     Implementation of reference counting for this type. Whenever an interface
00202 //     is requested, bump the refCount for the instance. NOTE: returning the
00203 //     refcount is a convention but is not required so don't rely on it.
00204 //
00205 ULONG MetadataImporterPluginAddRef(void *thisInstance)
00206 {
00207     ((MetadataImporterPluginType *)thisInstance )->refCount += 1;
00208     return ((MetadataImporterPluginType*) thisInstance)->refCount;
00209 }
00210 
00211 // -----------------------------------------------------------------------------
00212 // SampleCMPluginRelease
00213 // -----------------------------------------------------------------------------
00214 //     When an interface is released, decrement the refCount.
00215 //     If the refCount goes to zero, deallocate the instance.
00216 //
00217 ULONG MetadataImporterPluginRelease(void *thisInstance)
00218 {
00219     ((MetadataImporterPluginType*)thisInstance)->refCount -= 1;
00220     if (((MetadataImporterPluginType*)thisInstance)->refCount == 0){
00221         DeallocMetadataImporterPluginType((MetadataImporterPluginType*)thisInstance );
00222         return 0;
00223     }else{
00224         return ((MetadataImporterPluginType*) thisInstance )->refCount;
00225     }
00226 }
00227 
00228 // -----------------------------------------------------------------------------
00229 //     TBSpotlightMDImporterPluginFactory
00230 // -----------------------------------------------------------------------------
00231 //     Implementation of the factory function for this type.
00232 //
00233 void *MetadataImporterPluginFactory(CFAllocatorRef allocator,CFUUIDRef typeID)
00234 {
00235     MetadataImporterPluginType *result;
00236     CFUUIDRef                 uuid;
00237 
00238         /* If correct type is being requested, allocate an
00239          * instance of TestType and return the IUnknown interface.
00240          */
00241     if (CFEqual(typeID,kMDImporterTypeID)){
00242         uuid = CFUUIDCreateFromString(kCFAllocatorDefault,CFSTR(PLUGIN_ID));
00243         result = AllocMetadataImporterPluginType(uuid);
00244         CFRelease(uuid);
00245         return result;
00246     }
00247         /* If the requested type is incorrect, return NULL. */
00248     return NULL;
00249 }
00250