Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / doc / sfxmodelfactory.cxx
blob7a2441bfe1e8c38ccbcdea835811935124604e23
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sfx2/sfxmodelfactory.hxx"
31 /** === begin UNO includes === **/
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/beans/NamedValue.hpp>
34 #include <com/sun/star/lang/XInitialization.hpp>
35 /** === end UNO includes === **/
37 #include <comphelper/namedvaluecollection.hxx>
38 #include <cppuhelper/implbase2.hxx>
40 #include <algorithm>
41 #include <functional>
43 //........................................................................
44 namespace sfx2
46 //........................................................................
48 /** === begin UNO using === **/
49 using ::com::sun::star::uno::Reference;
50 using ::com::sun::star::uno::XInterface;
51 using ::com::sun::star::uno::UNO_QUERY;
52 using ::com::sun::star::uno::UNO_QUERY_THROW;
53 using ::com::sun::star::uno::UNO_SET_THROW;
54 using ::com::sun::star::uno::Exception;
55 using ::com::sun::star::uno::RuntimeException;
56 using ::com::sun::star::uno::Any;
57 using ::com::sun::star::uno::makeAny;
58 using ::com::sun::star::lang::XMultiServiceFactory;
59 using ::com::sun::star::uno::Sequence;
60 using ::com::sun::star::lang::XSingleServiceFactory;
61 using ::com::sun::star::lang::XServiceInfo;
62 using ::com::sun::star::beans::NamedValue;
63 using ::com::sun::star::beans::PropertyValue;
64 using ::com::sun::star::lang::XInitialization;
65 /** === end UNO using === **/
67 //====================================================================
68 //= SfxModelFactory - declaration
69 //====================================================================
70 typedef ::cppu::WeakImplHelper2 < XSingleServiceFactory
71 , XServiceInfo
72 > SfxModelFactory_Base;
73 /** implements a XSingleServiceFactory which can be used to created instances
74 of classes derived from SfxBaseModel
76 In opposite to the default implementations from module cppuhelper, this
77 factory evaluates certain creation arguments (passed to createInstanceWithArguments)
78 and passes them to the factory function of the derived class.
80 class SfxModelFactory : public SfxModelFactory_Base
82 public:
83 SfxModelFactory(
84 const Reference< XMultiServiceFactory >& _rxServiceFactory,
85 const ::rtl::OUString& _rImplementationName,
86 const SfxModelFactoryFunc _pComponentFactoryFunc,
87 const Sequence< ::rtl::OUString >& _rServiceNames
90 // XSingleServiceFactory
91 virtual Reference< XInterface > SAL_CALL createInstance( ) throw (Exception, RuntimeException);
92 virtual Reference< XInterface > SAL_CALL createInstanceWithArguments( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException);
94 // XServiceInfo
95 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException);
96 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException);
97 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException);
99 protected:
100 virtual ~SfxModelFactory();
102 private:
103 Reference< XInterface > impl_createInstance( const sal_uInt64 _nCreationFlags ) const;
105 private:
106 const Reference< XMultiServiceFactory > m_xServiceFactory;
107 const ::rtl::OUString m_sImplementationName;
108 const Sequence< ::rtl::OUString > m_aServiceNames;
109 const SfxModelFactoryFunc m_pComponentFactoryFunc;
112 //====================================================================
113 //= SfxModelFactory - implementation
114 //====================================================================
115 //--------------------------------------------------------------------
116 SfxModelFactory::SfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
117 const ::rtl::OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
118 const Sequence< ::rtl::OUString >& _rServiceNames )
119 :m_xServiceFactory( _rxServiceFactory )
120 ,m_sImplementationName( _rImplementationName )
121 ,m_aServiceNames( _rServiceNames )
122 ,m_pComponentFactoryFunc( _pComponentFactoryFunc )
126 //--------------------------------------------------------------------
127 SfxModelFactory::~SfxModelFactory()
131 //--------------------------------------------------------------------
132 Reference< XInterface > SfxModelFactory::impl_createInstance( const sal_uInt64 _nCreationFlags ) const
134 return (*m_pComponentFactoryFunc)( m_xServiceFactory, _nCreationFlags );
137 //--------------------------------------------------------------------
138 Reference< XInterface > SAL_CALL SfxModelFactory::createInstance( ) throw (Exception, RuntimeException)
140 return createInstanceWithArguments( Sequence< Any >() );
143 //--------------------------------------------------------------------
144 namespace
146 struct IsSpecialArgument : public ::std::unary_function< Any, bool >
148 static bool isSpecialArgumentName( const ::rtl::OUString& _rValueName )
150 return _rValueName == "EmbeddedObject" || _rValueName == "EmbeddedScriptSupport" || _rValueName == "DocumentRecoverySupport";
153 bool operator()( const Any& _rArgument ) const
155 NamedValue aNamedValue;
156 if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
157 return true;
158 PropertyValue aPropertyValue;
159 if ( ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name ) )
160 return true;
161 return false;
166 //--------------------------------------------------------------------
167 Reference< XInterface > SAL_CALL SfxModelFactory::createInstanceWithArguments( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
169 ::comphelper::NamedValueCollection aArgs( _rArguments );
170 const sal_Bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", sal_False );
171 const sal_Bool bScriptSupport = aArgs.getOrDefault( "EmbeddedScriptSupport", sal_True );
172 const sal_Bool bDocRecoverySupport = aArgs.getOrDefault( "DocumentRecoverySupport", sal_True );
174 sal_uInt64 nCreationFlags =
175 ( bEmbeddedObject ? SFXMODEL_EMBEDDED_OBJECT : 0 )
176 | ( bScriptSupport ? 0 : SFXMODEL_DISABLE_EMBEDDED_SCRIPTS )
177 | ( bDocRecoverySupport ? 0 : SFXMODEL_DISABLE_DOCUMENT_RECOVERY );
179 Reference< XInterface > xInstance( impl_createInstance( nCreationFlags ) );
181 // to mimic the bahaviour of the default factory's createInstanceWithArguments, we initialize
182 // the object with the given arguments, stripped by the three special ones
183 Sequence< Any > aStrippedArguments( _rArguments.getLength() );
184 Any* pStrippedArgs = aStrippedArguments.getArray();
185 Any* pStrippedArgsEnd = ::std::remove_copy_if(
186 _rArguments.getConstArray(),
187 _rArguments.getConstArray() + _rArguments.getLength(),
188 pStrippedArgs,
189 IsSpecialArgument()
191 aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
193 if ( aStrippedArguments.getLength() )
195 Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
196 OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
197 if ( xModelInit.is() )
198 xModelInit->initialize( aStrippedArguments );
201 return xInstance;
204 //--------------------------------------------------------------------
205 ::rtl::OUString SAL_CALL SfxModelFactory::getImplementationName( ) throw (RuntimeException)
207 return m_sImplementationName;
210 //--------------------------------------------------------------------
211 ::sal_Bool SAL_CALL SfxModelFactory::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
213 return ::std::find(
214 m_aServiceNames.getConstArray(),
215 m_aServiceNames.getConstArray() + m_aServiceNames.getLength(),
216 _rServiceName
217 ) != m_aServiceNames.getConstArray() + m_aServiceNames.getLength();
220 //--------------------------------------------------------------------
221 Sequence< ::rtl::OUString > SAL_CALL SfxModelFactory::getSupportedServiceNames( ) throw (RuntimeException)
223 return m_aServiceNames;
226 //--------------------------------------------------------------------
227 Reference< XSingleServiceFactory > createSfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
228 const ::rtl::OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
229 const Sequence< ::rtl::OUString >& _rServiceNames )
231 return new SfxModelFactory( _rxServiceFactory, _rImplementationName, _pComponentFactoryFunc, _rServiceNames );
234 //........................................................................
235 } // namespace sfx2
236 //........................................................................
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */