build fix
[LibreOffice.git] / sfx2 / source / doc / sfxmodelfactory.cxx
blob21a30bfd79e740708cfad3584832eac39cb4ebeb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sfx2/sfxmodelfactory.hxx>
22 #include <com/sun/star/lang/XServiceInfo.hpp>
23 #include <com/sun/star/beans/NamedValue.hpp>
24 #include <com/sun/star/lang/XInitialization.hpp>
26 #include <comphelper/namedvaluecollection.hxx>
27 #include <cppuhelper/implbase.hxx>
28 #include <cppuhelper/supportsservice.hxx>
30 #include <osl/diagnose.h>
32 #include <algorithm>
33 #include <functional>
36 namespace sfx2
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::XInterface;
42 using ::com::sun::star::uno::UNO_QUERY;
43 using ::com::sun::star::uno::Exception;
44 using ::com::sun::star::uno::RuntimeException;
45 using ::com::sun::star::uno::Any;
46 using ::com::sun::star::lang::XMultiServiceFactory;
47 using ::com::sun::star::uno::Sequence;
48 using ::com::sun::star::lang::XSingleServiceFactory;
49 using ::com::sun::star::lang::XServiceInfo;
50 using ::com::sun::star::beans::NamedValue;
51 using ::com::sun::star::beans::PropertyValue;
52 using ::com::sun::star::lang::XInitialization;
55 //= SfxModelFactory - declaration
57 typedef ::cppu::WeakImplHelper < XSingleServiceFactory
58 , XServiceInfo
59 > SfxModelFactory_Base;
60 /** implements a XSingleServiceFactory which can be used to created instances
61 of classes derived from SfxBaseModel
63 In opposite to the default implementations from module cppuhelper, this
64 factory evaluates certain creation arguments (passed to createInstanceWithArguments)
65 and passes them to the factory function of the derived class.
67 class SfxModelFactory : public SfxModelFactory_Base
69 public:
70 SfxModelFactory(
71 const Reference< XMultiServiceFactory >& _rxServiceFactory,
72 const OUString& _rImplementationName,
73 const SfxModelFactoryFunc _pComponentFactoryFunc,
74 const Sequence< OUString >& _rServiceNames
77 // XSingleServiceFactory
78 virtual Reference< XInterface > SAL_CALL createInstance( ) throw (Exception, RuntimeException, std::exception) override;
79 virtual Reference< XInterface > SAL_CALL createInstanceWithArguments( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException, std::exception) override;
81 // XServiceInfo
82 virtual OUString SAL_CALL getImplementationName( ) throw (RuntimeException, std::exception) override;
83 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw (RuntimeException, std::exception) override;
84 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException, std::exception) override;
86 protected:
87 virtual ~SfxModelFactory() override;
89 private:
90 const Reference< XMultiServiceFactory > m_xServiceFactory;
91 const OUString m_sImplementationName;
92 const Sequence< OUString > m_aServiceNames;
93 const SfxModelFactoryFunc m_pComponentFactoryFunc;
97 //= SfxModelFactory - implementation
100 SfxModelFactory::SfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
101 const OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
102 const Sequence< OUString >& _rServiceNames )
103 :m_xServiceFactory( _rxServiceFactory )
104 ,m_sImplementationName( _rImplementationName )
105 ,m_aServiceNames( _rServiceNames )
106 ,m_pComponentFactoryFunc( _pComponentFactoryFunc )
111 SfxModelFactory::~SfxModelFactory()
116 Reference< XInterface > SAL_CALL SfxModelFactory::createInstance( ) throw (Exception, RuntimeException, std::exception)
118 return createInstanceWithArguments( Sequence< Any >() );
122 namespace
124 struct IsSpecialArgument : public ::std::unary_function< Any, bool >
126 static bool isSpecialArgumentName( const OUString& _rValueName )
128 return _rValueName == "EmbeddedObject" || _rValueName == "EmbeddedScriptSupport" || _rValueName == "DocumentRecoverySupport";
131 bool operator()( const Any& _rArgument ) const
133 NamedValue aNamedValue;
134 if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
135 return true;
136 PropertyValue aPropertyValue;
137 if ( ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name ) )
138 return true;
139 return false;
145 Reference< XInterface > SAL_CALL SfxModelFactory::createInstanceWithArguments( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException, std::exception)
147 ::comphelper::NamedValueCollection aArgs( _rArguments );
148 const bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", false );
149 const bool bScriptSupport = aArgs.getOrDefault( "EmbeddedScriptSupport", true );
150 const bool bDocRecoverySupport = aArgs.getOrDefault( "DocumentRecoverySupport", true );
152 SfxModelFlags nCreationFlags =
153 ( bEmbeddedObject ? SfxModelFlags::EMBEDDED_OBJECT : SfxModelFlags::NONE )
154 | ( bScriptSupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS )
155 | ( bDocRecoverySupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_DOCUMENT_RECOVERY );
157 Reference< XInterface > xInstance( (*m_pComponentFactoryFunc)( m_xServiceFactory, nCreationFlags ) );
159 // to mimic the bahaviour of the default factory's createInstanceWithArguments, we initialize
160 // the object with the given arguments, stripped by the three special ones
161 Sequence< Any > aStrippedArguments( _rArguments.getLength() );
162 Any* pStrippedArgs = aStrippedArguments.getArray();
163 Any* pStrippedArgsEnd = ::std::remove_copy_if(
164 _rArguments.getConstArray(),
165 _rArguments.getConstArray() + _rArguments.getLength(),
166 pStrippedArgs,
167 IsSpecialArgument()
169 aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
171 if ( aStrippedArguments.getLength() )
173 Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
174 OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
175 if ( xModelInit.is() )
176 xModelInit->initialize( aStrippedArguments );
179 return xInstance;
182 OUString SAL_CALL SfxModelFactory::getImplementationName( ) throw (RuntimeException, std::exception)
184 return m_sImplementationName;
187 sal_Bool SAL_CALL SfxModelFactory::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
189 return cppu::supportsService(this, _rServiceName);
192 Sequence< OUString > SAL_CALL SfxModelFactory::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
194 return m_aServiceNames;
197 Reference< XSingleServiceFactory > createSfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
198 const OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
199 const Sequence< OUString >& _rServiceNames )
201 return new SfxModelFactory( _rxServiceFactory, _rImplementationName, _pComponentFactoryFunc, _rServiceNames );
203 } // namespace sfx2
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */