Back to index

lightning-sunbird  0.9+nobinonly
nsSanePluginFactory.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Rusty Lynch <rusty.lynch@intel.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 /*
00040  * Implements the SANE plugin factory class.
00041  */
00042 
00043 #include "nsString.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsSanePlugin_CID.h"
00047 #include "nsSanePlugin.h"
00048 #include "nsSanePluginFactory.h"
00049 #include "plstr.h"
00050 
00051 #define PLUGIN_NAME             "SANE Plugin"
00052 #define PLUGIN_DESCRIPTION      "SANE Plugin is a generic scanner interface"
00053 #define PLUGIN_MIME_DESCRIPTION "application/X-sane-plugin::Scanner/Camera"
00054 #define PLUGIN_MIME_TYPE        "application/X-sane-plugin"
00055 
00056 static NS_DEFINE_IID( kISupportsIID,            NS_ISUPPORTS_IID           );
00057 static NS_DEFINE_IID( kIFactoryIID,             NS_IFACTORY_IID            );
00058 static NS_DEFINE_IID( kIPluginIID,              NS_IPLUGIN_IID             );
00059 static NS_DEFINE_CID( kComponentManagerCID,     NS_COMPONENTMANAGER_CID    );
00060 static NS_DEFINE_CID( knsSanePluginControlCID,  NS_SANE_PLUGIN_CONTROL_CID );
00061 static NS_DEFINE_CID( knsSanePluginInst,        NS_SANE_PLUGIN_CID         );
00062 
00064 
00065 nsSanePluginFactoryImpl::nsSanePluginFactoryImpl( const nsCID &aClass,
00066                                                   const char* className,
00067                                                   const char* contractID )
00068     : mClassID(aClass), mClassName(className), mContractID(contractID)
00069 {
00070 #ifdef DEBUG
00071     printf("nsSanePluginFactoryImpl::nsSanePluginFactoryImpl()\n");
00072 #endif
00073 }
00074 
00075 nsSanePluginFactoryImpl::~nsSanePluginFactoryImpl()
00076 {
00077 #ifdef DEBUG
00078     printf("nsSanePluginFactoryImpl::~nsSanePluginFactoryImpl()\n");
00079 #endif
00080 
00081     printf("mRefCnt = %i\n", mRefCnt);
00082     NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction");
00083 }
00084 
00085 NS_IMETHODIMP
00086 nsSanePluginFactoryImpl::QueryInterface(const nsIID &aIID, 
00087                                         void **aInstancePtr)
00088 {
00089 #ifdef DEBUG
00090     printf("nsSanePluginFactoryImpl::QueryInterface()\n");
00091 #endif
00092 
00093     if (!aInstancePtr)
00094         return NS_ERROR_NULL_POINTER;
00095 
00096     if (aIID.Equals(kISupportsIID)) {
00097         *aInstancePtr = NS_STATIC_CAST(nsISupports*,this);
00098     } else if (aIID.Equals(kIFactoryIID)) {
00099         *aInstancePtr = NS_STATIC_CAST(nsISupports*,
00100                                        NS_STATIC_CAST(nsIFactory*,this));
00101     } else if (aIID.Equals(kIPluginIID)) {
00102         *aInstancePtr = NS_STATIC_CAST(nsISupports*,
00103                                        NS_STATIC_CAST(nsIPlugin*,this));
00104     } else {
00105         *aInstancePtr = nsnull;
00106         return NS_ERROR_NO_INTERFACE;
00107     }
00108 
00109     NS_ADDREF(NS_REINTERPRET_CAST(nsISupports*,*aInstancePtr));
00110 
00111     return NS_OK;
00112 }
00113 
00114 // Standard implementation of AddRef and Release
00115 NS_IMPL_ADDREF( nsSanePluginFactoryImpl )
00116 NS_IMPL_RELEASE( nsSanePluginFactoryImpl )
00117 
00118 NS_IMETHODIMP
00119 nsSanePluginFactoryImpl::CreateInstance( nsISupports *aOuter,
00120                                          const nsIID &aIID,
00121                                          void **aResult)
00122 {
00123 #ifdef DEBUG
00124     printf("nsSanePluginFactoryImpl::CreateInstance()\n");
00125 #endif
00126 
00127     if ( !aResult )
00128         return NS_ERROR_NULL_POINTER;
00129   
00130     if ( aOuter )
00131         return NS_ERROR_NO_AGGREGATION;
00132 
00133     nsSanePluginInstance * inst = new nsSanePluginInstance();
00134     if (!inst)
00135         return NS_ERROR_OUT_OF_MEMORY;
00136            
00137     inst->AddRef();
00138     *aResult = inst;
00139     return NS_OK;
00140 }
00141 
00142 nsresult 
00143 nsSanePluginFactoryImpl::LockFactory(PRBool aLock)
00144 {
00145 #ifdef DEBUG
00146     printf("nsSanePluginFactoryImpl::LockFactory()\n");
00147 #endif
00148 
00149     // Needs to be implemented
00150 
00151     return NS_OK;
00152 }
00153 
00154 
00155 NS_METHOD
00156 nsSanePluginFactoryImpl::Initialize()
00157 {
00158 #ifdef DEBUG
00159     printf("nsSanePluginFactoryImpl::Initialize()\n");
00160 #endif
00161 
00162     return NS_OK;
00163 }
00164 
00165 
00166 NS_METHOD
00167 nsSanePluginFactoryImpl::Shutdown( void )
00168 {
00169 #ifdef DEBUG
00170     printf("nsSanePluginFactoryImpl::Shutdown()\n");
00171 #endif
00172 
00173     return NS_OK;
00174 }
00175 
00176 
00177 NS_METHOD 
00178 nsSanePluginFactoryImpl::GetMIMEDescription(const char* *result)
00179 {
00180 #ifdef DEBUG
00181     printf("nsSanePluginFactoryImpl::GetMIMEDescription()\n");
00182 #endif
00183 
00184     // caller is responsible for releasing
00185     *result = PL_strdup( PLUGIN_MIME_DESCRIPTION );
00186 
00187     return NS_OK;
00188 }
00189 
00190 NS_METHOD
00191 nsSanePluginFactoryImpl::GetValue( nsPluginVariable variable, void *value )
00192 {
00193 #ifdef DEBUG
00194     printf("nsSanePluginFactoryImpl::GetValue()\n");
00195 #endif
00196 
00197     nsresult err = NS_OK;
00198 
00199     if ( variable == nsPluginVariable_NameString ) {
00200     
00201         *( ( char ** )value ) = strdup( PLUGIN_NAME );
00202 
00203     } else if ( variable == nsPluginVariable_DescriptionString ) {
00204 
00205         *( ( char ** )value ) = strdup( PLUGIN_DESCRIPTION );
00206 
00207     } else {
00208     
00209         err = NS_ERROR_FAILURE;
00210 
00211     }
00212   
00213     return err;
00214 }
00215 
00216 
00217 NS_IMETHODIMP 
00218 nsSanePluginFactoryImpl::CreatePluginInstance( nsISupports *aOuter, 
00219                                                REFNSIID aIID, 
00220                                                const char* aPluginMIMEType,
00221                                                void **aResult)
00222 {
00223 #ifdef DEBUG
00224     printf("nsSanePluginFactoryImpl::CreatePluginInstance()\n");
00225 #endif
00226 
00227     // Need to find out what this is used for.  The npsimple
00228     // plugin looks like it just does a CreateInstance and 
00229     // ignores the mime type.
00230     return NS_ERROR_NOT_IMPLEMENTED;
00231 }
00232 
00233 
00235 
00243 extern "C" PR_IMPLEMENT(nsresult)
00244 NSGetFactory( nsISupports* aServMgr,
00245               const nsCID &aClass,
00246               const char *aClassName,
00247               const char *aContractID,
00248               nsIFactory **aFactory)
00249 {
00250     if (! aFactory)
00251         return NS_ERROR_NULL_POINTER;
00252   
00253     nsSanePluginFactoryImpl* factory = new nsSanePluginFactoryImpl(aClass, 
00254                                                                    aClassName,
00255                                                                    aContractID);
00256     if ( factory == nsnull )
00257         return NS_ERROR_OUT_OF_MEMORY;
00258   
00259     NS_ADDREF(factory);
00260     *aFactory = factory;
00261     return NS_OK;
00262 
00263 }
00264 
00265 char *buf;
00266 
00267 extern "C" PR_IMPLEMENT( nsresult )
00268 NSRegisterSelf( nsISupports* aServMgr, const char* aPath )
00269 {
00270     nsresult rv;
00271   
00272     nsCOMPtr<nsIServiceManager> servMgr( do_QueryInterface( aServMgr, &rv ) );
00273     if ( NS_FAILED( rv ) )
00274         return rv;
00275   
00276     nsCOMPtr<nsIComponentManager> compMgr = 
00277              do_GetService( kComponentManagerCID, &rv );
00278     if ( NS_FAILED( rv ) )
00279         return rv;
00280   
00281     // Register the plugin control portion.
00282     rv = compMgr->RegisterComponent(knsSanePluginControlCID,
00283                                     "SANE Plugin Control",
00284                                     "@mozilla.org/plugins/sane-control;1",
00285                                     aPath, PR_TRUE, PR_TRUE );
00286   
00287     // Register the plugin portion.
00288     nsString contractID;
00289     contractID.AssignWithConversion( NS_INLINE_PLUGIN_CONTRACTID_PREFIX );
00290 
00291     contractID.AppendWithConversion(PLUGIN_MIME_TYPE);
00292     buf = ( char * )calloc( 2000, sizeof( char ) );
00293     contractID.ToCString( buf, 1999 );
00294   
00295     rv = compMgr->RegisterComponent( knsSanePluginInst,
00296                                      "SANE Plugin Component",
00297                                      buf,
00298                                      aPath, PR_TRUE, PR_TRUE);
00299     free( buf );
00300   
00301     if ( NS_FAILED( rv ) )
00302         return rv;
00303   
00304     return NS_OK;
00305 
00306 }
00307 
00308 extern "C" PR_IMPLEMENT( nsresult )
00309 NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
00310 {
00311     nsresult rv;
00312   
00313     nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
00314     if (NS_FAILED(rv)) return rv;
00315   
00316     nsCOMPtr<nsIComponentManager> compMgr = 
00317              do_GetService(kComponentManagerCID, &rv);
00318     if (NS_FAILED(rv)) return rv;
00319   
00320     rv = compMgr->UnregisterComponent(knsSanePluginControlCID, aPath);
00321     if (NS_FAILED(rv)) return rv;
00322   
00323     return NS_OK;
00324 }
00325 
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342