File:  [mozdev] / abimoz / plugin / nsWPPluginFactory.cpp
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Fri Oct 25 14:42:28 2002 UTC (14 years, 11 months ago) by mikep
Branches: MAIN
CVS tags: HEAD
Upgraded to Mozilla 1.2b and abiword 1.0.3.

/*
This file is part of AbiMoz.

nsWPPluginControl.idl - Implements the WP plugin factory class.
Copyright (C) 2001 OEone Corporation

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

The author of this program can be contacted in writing at
OEone Corporation, 290 St-Joseph Blvd., Hull, QC J8Y 3Y3, CANADA.
*/


#include "nsString.h"
#include "nsIServiceManager.h"
#include "nsWPPlugin.h"
#include "nsWPPluginFactory.h"
#include "plstr.h"

extern void pluginCommand( void *, int c, void * );
#define PL_SHUTDOWN             100

#define NS_WP_PLUGIN_CID { 0x322da841, 0x3dbf, 0x11d3, { 0xbc, 0xfb, 0x0, 0xa0, 0xc9, 0xc8, 0xd9, 0x1d } }
#define PLUGIN_NAME             "WordProcessor Plugin"
#define PLUGIN_DESCRIPTION      "WordProcessor Plugin is a word processor controled with javascript"
#define PLUGIN_MIME_DESCRIPTION "application/msword::Microsoft Word Documents"
#define PLUGIN_MIME_TYPE        "application/msword"

static NS_DEFINE_IID( kISupportsIID,            NS_ISUPPORTS_IID           );
static NS_DEFINE_IID( kIFactoryIID,             NS_IFACTORY_IID            );
static NS_DEFINE_IID( kIPluginIID,              NS_IPLUGIN_IID             );
static NS_DEFINE_CID( kComponentManagerCID,     NS_COMPONENTMANAGER_CID    );
static NS_DEFINE_CID( knsWPPluginControlCID,    NS_WP_PLUGIN_CONTROL_CID   );
static NS_DEFINE_CID( knsWPPluginInst,          NS_WP_PLUGIN_CID           );

////////////////////////////////////////////////////////////////////////

nsWPPluginFactoryImpl::nsWPPluginFactoryImpl( const nsCID &aClass,
                                                  const char* className,
                                                  const char* contractID )
    : mClassID(aClass), mClassName(className), mContractID(contractID)
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::nsWPPluginFactoryImpl()\n");
#endif

    NS_INIT_REFCNT();
}

nsWPPluginFactoryImpl::~nsWPPluginFactoryImpl()
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::~nsWPPluginFactoryImpl()\n");
#endif

//    printf("mRefCnt = %i\n", mRefCnt );

    pluginCommand( NULL, PL_SHUTDOWN, NULL );
    NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction");
}

NS_IMETHODIMP
nsWPPluginFactoryImpl::QueryInterface(const nsIID &aIID, 
                                        void **aInstancePtr)
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::QueryInterface()\n");
#endif

    if (!aInstancePtr)
        return NS_ERROR_NULL_POINTER;

    if (aIID.Equals(kISupportsIID)) {
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,this);
    } else if (aIID.Equals(kIFactoryIID)) {
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,
                                       NS_STATIC_CAST(nsIFactory*,this));
    } else if (aIID.Equals(kIPluginIID)) {
        *aInstancePtr = NS_STATIC_CAST(nsISupports*,
                                       NS_STATIC_CAST(nsIPlugin*,this));
    } else {
        *aInstancePtr = nsnull;
        return NS_ERROR_NO_INTERFACE;
    }

    NS_ADDREF(NS_REINTERPRET_CAST(nsISupports*,*aInstancePtr));

    return NS_OK;
}

// Standard implementation of AddRef and Release
NS_IMPL_ADDREF( nsWPPluginFactoryImpl );
NS_IMPL_RELEASE( nsWPPluginFactoryImpl );

NS_IMETHODIMP
nsWPPluginFactoryImpl::CreateInstance( nsISupports *aOuter,
                                         const nsIID &aIID,
                                         void **aResult)
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::CreateInstance()\n");
#endif

    if ( !aResult )
        return NS_ERROR_NULL_POINTER;
  
    if ( aOuter )
        return NS_ERROR_NO_AGGREGATION;

    nsWPPluginInstance * inst = new nsWPPluginInstance();
    if (!inst)
        return NS_ERROR_OUT_OF_MEMORY;
           
    inst->AddRef();
    *aResult = inst;
    return NS_OK;
}

nsresult 
nsWPPluginFactoryImpl::LockFactory(PRBool aLock)
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::LockFactory()\n");
#endif

    // Needs to be implemented

    return NS_OK;
}

NS_METHOD
nsWPPluginFactoryImpl::Initialize()
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::Initialize()\n");
#endif

    return NS_OK;
}


NS_METHOD
nsWPPluginFactoryImpl::Shutdown( void )
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::Shutdown()\n");
#endif

    return NS_OK;
}


NS_METHOD 
nsWPPluginFactoryImpl::GetMIMEDescription(const char* *result)
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::GetMIMEDescription()\n");
#endif

    // caller is responsible for releasing
    *result = PL_strdup( PLUGIN_MIME_DESCRIPTION );

    return NS_OK;
}

NS_METHOD
nsWPPluginFactoryImpl::GetValue( nsPluginVariable variable, void *value )
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::GetValue(%d)\n", variable);
#endif

    nsresult err = NS_OK;

    if ( variable == nsPluginVariable_NameString ) {
    
        *( ( char ** )value ) = strdup( PLUGIN_NAME );

    } else if ( variable == nsPluginVariable_DescriptionString ) {

        *( ( char ** )value ) = strdup( PLUGIN_DESCRIPTION );

    } else {
    
        err = NS_ERROR_FAILURE;

    }
  
    return err;
}


NS_IMETHODIMP 
nsWPPluginFactoryImpl::CreatePluginInstance( nsISupports *aOuter, 
                                               REFNSIID aIID, 
                                               const char* aPluginMIMEType,
                                               void **aResult)
{
#ifdef PLUGIN_DEBUG
    printf("nsWPPluginFactoryImpl::CreatePluginInstance()\n");
#endif

    // Need to find out what this is used for.  The npsimple
    // plugin looks like it just does a CreateInstance and 
    // ignores the mime type.
    return NS_ERROR_NOT_IMPLEMENTED;
}


////////////////////////////////////////////////////////////////////////

/**
 * The XPCOM runtime will call this to get a new factory object for the
 * CID/contractID it passes in.  XPCOM is responsible for caching the resulting
 * factory.
 *
 * return the proper factory to the caller
 */
extern "C" PR_IMPLEMENT(nsresult)
NSGetFactory( nsISupports* aServMgr,
              const nsCID &aClass,
              const char *aClassName,
              const char *aContractID,
              nsIFactory **aFactory)
{
#ifdef PLUGIN_DEBUG
    printf("NSGetFactory()\n");
#endif
    if (! aFactory)
        return NS_ERROR_NULL_POINTER;
  
    nsWPPluginFactoryImpl* factory = new nsWPPluginFactoryImpl(aClass, 
                                                                   aClassName,
                                                                   aContractID);
    if ( factory == nsnull )
        return NS_ERROR_OUT_OF_MEMORY;
  
    NS_ADDREF(factory);
    *aFactory = factory;
    return NS_OK;

}

char *buf;

extern "C" PR_IMPLEMENT( nsresult )
NSRegisterSelf( nsISupports* aServMgr, const char* aPath )
{
    nsresult rv;
    printf( "Registering WPPlugin...\n" );
    nsCOMPtr<nsIServiceManager> servMgr( do_QueryInterface( aServMgr, &rv ) );
    if ( NS_FAILED( rv ) )
        return rv;
  
    nsCOMPtr<nsIComponentManager> compMgr = 
             do_GetService( kComponentManagerCID, &rv );
    if ( NS_FAILED( rv ) )
        return rv;
  
    // Register the plugin control portion.
    rv = compMgr->RegisterComponent(knsWPPluginControlCID,
                                    "WordProcessor Plugin Control",
                                    "@mozilla.org/plugins/wordp_control;1",
                                    aPath, PR_TRUE, PR_TRUE );
 
    // Register the plugin portion.
    nsString contractID;
    contractID.AssignWithConversion( NS_INLINE_PLUGIN_CONTRACTID_PREFIX );

    contractID.AppendWithConversion(PLUGIN_MIME_TYPE);
    buf = ( char * )calloc( 2000, sizeof( char ) );
    contractID.ToCString( buf, 1999 );
  
    rv = compMgr->RegisterComponent( knsWPPluginInst,
                                     "WordProcessor Plugin Component",
                                     buf,
                                     aPath, PR_TRUE, PR_TRUE);
    free( buf );
  
    if ( NS_FAILED( rv ) )
        return rv;
  
    printf( "Registering Complete.\n" );
    return NS_OK;
}

extern "C" PR_IMPLEMENT( nsresult )
NSUnregisterSelf(nsISupports* aServMgr, const char* aPath)
{
    nsresult rv;
  
    nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
    if (NS_FAILED(rv)) return rv;
  
    nsCOMPtr<nsIComponentManager> compMgr = 
             do_GetService( kComponentManagerCID, &rv );
    if (NS_FAILED(rv)) return rv;
  
    rv = compMgr->UnregisterComponent(knsWPPluginControlCID, aPath);
    if (NS_FAILED(rv)) return rv;
  
    return NS_OK;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>