1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_sfx2.hxx" 30*cdf0e10cSrcweir #include "sfx2/sfxmodelfactory.hxx" 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir /** === begin UNO includes === **/ 33*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp> 36*cdf0e10cSrcweir /** === end UNO includes === **/ 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx> 39*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir #include <algorithm> 42*cdf0e10cSrcweir #include <functional> 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir //........................................................................ 45*cdf0e10cSrcweir namespace sfx2 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir //........................................................................ 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir /** === begin UNO using === **/ 50*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 51*cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 52*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 53*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 54*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 55*cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 56*cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 57*cdf0e10cSrcweir using ::com::sun::star::uno::Any; 58*cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 59*cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory; 60*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 61*cdf0e10cSrcweir using ::com::sun::star::lang::XSingleServiceFactory; 62*cdf0e10cSrcweir using ::com::sun::star::lang::XServiceInfo; 63*cdf0e10cSrcweir using ::com::sun::star::beans::NamedValue; 64*cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue; 65*cdf0e10cSrcweir using ::com::sun::star::lang::XInitialization; 66*cdf0e10cSrcweir /** === end UNO using === **/ 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir //==================================================================== 69*cdf0e10cSrcweir //= SfxModelFactory - declaration 70*cdf0e10cSrcweir //==================================================================== 71*cdf0e10cSrcweir typedef ::cppu::WeakImplHelper2 < XSingleServiceFactory 72*cdf0e10cSrcweir , XServiceInfo 73*cdf0e10cSrcweir > SfxModelFactory_Base; 74*cdf0e10cSrcweir /** implements a XSingleServiceFactory which can be used to created instances 75*cdf0e10cSrcweir of classes derived from SfxBaseModel 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir In opposite to the default implementations from module cppuhelper, this 78*cdf0e10cSrcweir factory evaluates certain creation arguments (passed to createInstanceWithArguments) 79*cdf0e10cSrcweir and passes them to the factory function of the derived class. 80*cdf0e10cSrcweir */ 81*cdf0e10cSrcweir class SfxModelFactory : public SfxModelFactory_Base 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir public: 84*cdf0e10cSrcweir SfxModelFactory( 85*cdf0e10cSrcweir const Reference< XMultiServiceFactory >& _rxServiceFactory, 86*cdf0e10cSrcweir const ::rtl::OUString& _rImplementationName, 87*cdf0e10cSrcweir const SfxModelFactoryFunc _pComponentFactoryFunc, 88*cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames 89*cdf0e10cSrcweir ); 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir // XSingleServiceFactory 92*cdf0e10cSrcweir virtual Reference< XInterface > SAL_CALL createInstance( ) throw (Exception, RuntimeException); 93*cdf0e10cSrcweir virtual Reference< XInterface > SAL_CALL createInstanceWithArguments( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException); 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir // XServiceInfo 96*cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException); 97*cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException); 98*cdf0e10cSrcweir virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException); 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir protected: 101*cdf0e10cSrcweir virtual ~SfxModelFactory(); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir private: 104*cdf0e10cSrcweir Reference< XInterface > impl_createInstance( const sal_uInt64 _nCreationFlags ) const; 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir private: 107*cdf0e10cSrcweir const Reference< XMultiServiceFactory > m_xServiceFactory; 108*cdf0e10cSrcweir const ::rtl::OUString m_sImplementationName; 109*cdf0e10cSrcweir const Sequence< ::rtl::OUString > m_aServiceNames; 110*cdf0e10cSrcweir const SfxModelFactoryFunc m_pComponentFactoryFunc; 111*cdf0e10cSrcweir }; 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir //==================================================================== 114*cdf0e10cSrcweir //= SfxModelFactory - implementation 115*cdf0e10cSrcweir //==================================================================== 116*cdf0e10cSrcweir //-------------------------------------------------------------------- 117*cdf0e10cSrcweir SfxModelFactory::SfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory, 118*cdf0e10cSrcweir const ::rtl::OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc, 119*cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames ) 120*cdf0e10cSrcweir :m_xServiceFactory( _rxServiceFactory ) 121*cdf0e10cSrcweir ,m_sImplementationName( _rImplementationName ) 122*cdf0e10cSrcweir ,m_aServiceNames( _rServiceNames ) 123*cdf0e10cSrcweir ,m_pComponentFactoryFunc( _pComponentFactoryFunc ) 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir //-------------------------------------------------------------------- 128*cdf0e10cSrcweir SfxModelFactory::~SfxModelFactory() 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir //-------------------------------------------------------------------- 133*cdf0e10cSrcweir Reference< XInterface > SfxModelFactory::impl_createInstance( const sal_uInt64 _nCreationFlags ) const 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir return (*m_pComponentFactoryFunc)( m_xServiceFactory, _nCreationFlags ); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir //-------------------------------------------------------------------- 139*cdf0e10cSrcweir Reference< XInterface > SAL_CALL SfxModelFactory::createInstance( ) throw (Exception, RuntimeException) 140*cdf0e10cSrcweir { 141*cdf0e10cSrcweir return createInstanceWithArguments( Sequence< Any >() ); 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir //-------------------------------------------------------------------- 145*cdf0e10cSrcweir namespace 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir struct IsSpecialArgument : public ::std::unary_function< Any, bool > 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir static bool isSpecialArgumentName( const ::rtl::OUString& _rValueName ) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir return _rValueName.equalsAscii( "EmbeddedObject" ) 152*cdf0e10cSrcweir || _rValueName.equalsAscii( "EmbeddedScriptSupport" ) 153*cdf0e10cSrcweir || _rValueName.equalsAscii( "DocumentRecoverySupport" ); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir bool operator()( const Any& _rArgument ) const 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir NamedValue aNamedValue; 159*cdf0e10cSrcweir if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) ) 160*cdf0e10cSrcweir return true; 161*cdf0e10cSrcweir PropertyValue aPropertyValue; 162*cdf0e10cSrcweir if ( ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name ) ) 163*cdf0e10cSrcweir return true; 164*cdf0e10cSrcweir return false; 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir }; 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir //-------------------------------------------------------------------- 170*cdf0e10cSrcweir Reference< XInterface > SAL_CALL SfxModelFactory::createInstanceWithArguments( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir ::comphelper::NamedValueCollection aArgs( _rArguments ); 173*cdf0e10cSrcweir const sal_Bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", sal_False ); 174*cdf0e10cSrcweir const sal_Bool bScriptSupport = aArgs.getOrDefault( "EmbeddedScriptSupport", sal_True ); 175*cdf0e10cSrcweir const sal_Bool bDocRecoverySupport = aArgs.getOrDefault( "DocumentRecoverySupport", sal_True ); 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir sal_uInt64 nCreationFlags = 178*cdf0e10cSrcweir ( bEmbeddedObject ? SFXMODEL_EMBEDDED_OBJECT : 0 ) 179*cdf0e10cSrcweir | ( bScriptSupport ? 0 : SFXMODEL_DISABLE_EMBEDDED_SCRIPTS ) 180*cdf0e10cSrcweir | ( bDocRecoverySupport ? 0 : SFXMODEL_DISABLE_DOCUMENT_RECOVERY ); 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir Reference< XInterface > xInstance( impl_createInstance( nCreationFlags ) ); 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir // to mimic the bahaviour of the default factory's createInstanceWithArguments, we initialize 185*cdf0e10cSrcweir // the object with the given arguments, stripped by the three special ones 186*cdf0e10cSrcweir Sequence< Any > aStrippedArguments( _rArguments.getLength() ); 187*cdf0e10cSrcweir Any* pStrippedArgs = aStrippedArguments.getArray(); 188*cdf0e10cSrcweir Any* pStrippedArgsEnd = ::std::remove_copy_if( 189*cdf0e10cSrcweir _rArguments.getConstArray(), 190*cdf0e10cSrcweir _rArguments.getConstArray() + _rArguments.getLength(), 191*cdf0e10cSrcweir pStrippedArgs, 192*cdf0e10cSrcweir IsSpecialArgument() 193*cdf0e10cSrcweir ); 194*cdf0e10cSrcweir aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs ); 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir if ( aStrippedArguments.getLength() ) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir Reference< XInitialization > xModelInit( xInstance, UNO_QUERY ); 199*cdf0e10cSrcweir OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" ); 200*cdf0e10cSrcweir if ( xModelInit.is() ) 201*cdf0e10cSrcweir xModelInit->initialize( aStrippedArguments ); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir return xInstance; 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir //-------------------------------------------------------------------- 208*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SfxModelFactory::getImplementationName( ) throw (RuntimeException) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir return m_sImplementationName; 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir //-------------------------------------------------------------------- 214*cdf0e10cSrcweir ::sal_Bool SAL_CALL SfxModelFactory::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir return ::std::find( 217*cdf0e10cSrcweir m_aServiceNames.getConstArray(), 218*cdf0e10cSrcweir m_aServiceNames.getConstArray() + m_aServiceNames.getLength(), 219*cdf0e10cSrcweir _rServiceName 220*cdf0e10cSrcweir ) != m_aServiceNames.getConstArray() + m_aServiceNames.getLength(); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir //-------------------------------------------------------------------- 224*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL SfxModelFactory::getSupportedServiceNames( ) throw (RuntimeException) 225*cdf0e10cSrcweir { 226*cdf0e10cSrcweir return m_aServiceNames; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir //-------------------------------------------------------------------- 230*cdf0e10cSrcweir Reference< XSingleServiceFactory > createSfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory, 231*cdf0e10cSrcweir const ::rtl::OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc, 232*cdf0e10cSrcweir const Sequence< ::rtl::OUString >& _rServiceNames ) 233*cdf0e10cSrcweir { 234*cdf0e10cSrcweir return new SfxModelFactory( _rxServiceFactory, _rImplementationName, _pComponentFactoryFunc, _rServiceNames ); 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir //........................................................................ 238*cdf0e10cSrcweir } // namespace sfx2 239*cdf0e10cSrcweir //........................................................................ 240