Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include "sfx2/sfxmodelfactory.hxx"
21 : :
22 : : #include <com/sun/star/lang/XServiceInfo.hpp>
23 : : #include <com/sun/star/beans/NamedValue.hpp>
24 : : #include <com/sun/star/lang/XInitialization.hpp>
25 : :
26 : : #include <comphelper/namedvaluecollection.hxx>
27 : : #include <cppuhelper/implbase2.hxx>
28 : :
29 : : #include <algorithm>
30 : : #include <functional>
31 : :
32 : : //........................................................................
33 : : namespace sfx2
34 : : {
35 : : //........................................................................
36 : :
37 : : /** === begin UNO using === **/
38 : : using ::com::sun::star::uno::Reference;
39 : : using ::com::sun::star::uno::XInterface;
40 : : using ::com::sun::star::uno::UNO_QUERY;
41 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
42 : : using ::com::sun::star::uno::UNO_SET_THROW;
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::uno::makeAny;
47 : : using ::com::sun::star::lang::XMultiServiceFactory;
48 : : using ::com::sun::star::uno::Sequence;
49 : : using ::com::sun::star::lang::XSingleServiceFactory;
50 : : using ::com::sun::star::lang::XServiceInfo;
51 : : using ::com::sun::star::beans::NamedValue;
52 : : using ::com::sun::star::beans::PropertyValue;
53 : : using ::com::sun::star::lang::XInitialization;
54 : : /** === end UNO using === **/
55 : :
56 : : //====================================================================
57 : : //= SfxModelFactory - declaration
58 : : //====================================================================
59 : : typedef ::cppu::WeakImplHelper2 < XSingleServiceFactory
60 : : , XServiceInfo
61 : : > SfxModelFactory_Base;
62 : : /** implements a XSingleServiceFactory which can be used to created instances
63 : : of classes derived from SfxBaseModel
64 : :
65 : : In opposite to the default implementations from module cppuhelper, this
66 : : factory evaluates certain creation arguments (passed to createInstanceWithArguments)
67 : : and passes them to the factory function of the derived class.
68 : : */
69 : : class SfxModelFactory : public SfxModelFactory_Base
70 : : {
71 : : public:
72 : : SfxModelFactory(
73 : : const Reference< XMultiServiceFactory >& _rxServiceFactory,
74 : : const ::rtl::OUString& _rImplementationName,
75 : : const SfxModelFactoryFunc _pComponentFactoryFunc,
76 : : const Sequence< ::rtl::OUString >& _rServiceNames
77 : : );
78 : :
79 : : // XSingleServiceFactory
80 : : virtual Reference< XInterface > SAL_CALL createInstance( ) throw (Exception, RuntimeException);
81 : : virtual Reference< XInterface > SAL_CALL createInstanceWithArguments( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException);
82 : :
83 : : // XServiceInfo
84 : : virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException);
85 : : virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException);
86 : : virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException);
87 : :
88 : : protected:
89 : : virtual ~SfxModelFactory();
90 : :
91 : : private:
92 : : Reference< XInterface > impl_createInstance( const sal_uInt64 _nCreationFlags ) const;
93 : :
94 : : private:
95 : : const Reference< XMultiServiceFactory > m_xServiceFactory;
96 : : const ::rtl::OUString m_sImplementationName;
97 : : const Sequence< ::rtl::OUString > m_aServiceNames;
98 : : const SfxModelFactoryFunc m_pComponentFactoryFunc;
99 : : };
100 : :
101 : : //====================================================================
102 : : //= SfxModelFactory - implementation
103 : : //====================================================================
104 : : //--------------------------------------------------------------------
105 : 158 : SfxModelFactory::SfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
106 : : const ::rtl::OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
107 : : const Sequence< ::rtl::OUString >& _rServiceNames )
108 : : :m_xServiceFactory( _rxServiceFactory )
109 : : ,m_sImplementationName( _rImplementationName )
110 : : ,m_aServiceNames( _rServiceNames )
111 [ + - ]: 158 : ,m_pComponentFactoryFunc( _pComponentFactoryFunc )
112 : : {
113 : 158 : }
114 : :
115 : : //--------------------------------------------------------------------
116 [ + - ]: 158 : SfxModelFactory::~SfxModelFactory()
117 : : {
118 [ - + ]: 316 : }
119 : :
120 : : //--------------------------------------------------------------------
121 : 2451 : Reference< XInterface > SfxModelFactory::impl_createInstance( const sal_uInt64 _nCreationFlags ) const
122 : : {
123 : 2451 : return (*m_pComponentFactoryFunc)( m_xServiceFactory, _nCreationFlags );
124 : : }
125 : :
126 : : //--------------------------------------------------------------------
127 : 2025 : Reference< XInterface > SAL_CALL SfxModelFactory::createInstance( ) throw (Exception, RuntimeException)
128 : : {
129 [ + - ]: 2025 : return createInstanceWithArguments( Sequence< Any >() );
130 : : }
131 : :
132 : : //--------------------------------------------------------------------
133 : : namespace
134 : : {
135 : : struct IsSpecialArgument : public ::std::unary_function< Any, bool >
136 : : {
137 : 1278 : static bool isSpecialArgumentName( const ::rtl::OUString& _rValueName )
138 : : {
139 [ + + ][ + + ]: 1278 : return _rValueName == "EmbeddedObject" || _rValueName == "EmbeddedScriptSupport" || _rValueName == "DocumentRecoverySupport";
[ + - ]
140 : : }
141 : :
142 : 1278 : bool operator()( const Any& _rArgument ) const
143 : : {
144 : 1278 : NamedValue aNamedValue;
145 [ - + ][ # # ]: 1278 : if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
[ # # ][ - + ]
[ + - ]
146 : 0 : return true;
147 : 1278 : PropertyValue aPropertyValue;
148 [ + - ][ + - ]: 1278 : if ( ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name ) )
[ + - ][ + - ]
[ + - ]
149 : 1278 : return true;
150 : 1278 : return false;
151 : : }
152 : : };
153 : : }
154 : :
155 : : //--------------------------------------------------------------------
156 : 2451 : Reference< XInterface > SAL_CALL SfxModelFactory::createInstanceWithArguments( const Sequence< Any >& _rArguments ) throw (Exception, RuntimeException)
157 : : {
158 [ + - ]: 2451 : ::comphelper::NamedValueCollection aArgs( _rArguments );
159 [ + - ]: 2451 : const sal_Bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", sal_False );
160 [ + - ]: 2451 : const sal_Bool bScriptSupport = aArgs.getOrDefault( "EmbeddedScriptSupport", sal_True );
161 [ + - ]: 2451 : const sal_Bool bDocRecoverySupport = aArgs.getOrDefault( "DocumentRecoverySupport", sal_True );
162 : :
163 : : sal_uInt64 nCreationFlags =
164 : : ( bEmbeddedObject ? SFXMODEL_EMBEDDED_OBJECT : 0 )
165 : : | ( bScriptSupport ? 0 : SFXMODEL_DISABLE_EMBEDDED_SCRIPTS )
166 [ + + ][ + - ]: 2451 : | ( bDocRecoverySupport ? 0 : SFXMODEL_DISABLE_DOCUMENT_RECOVERY );
[ + - ]
167 : :
168 [ + - ]: 2451 : Reference< XInterface > xInstance( impl_createInstance( nCreationFlags ) );
169 : :
170 : : // to mimic the bahaviour of the default factory's createInstanceWithArguments, we initialize
171 : : // the object with the given arguments, stripped by the three special ones
172 [ + - ]: 2451 : Sequence< Any > aStrippedArguments( _rArguments.getLength() );
173 [ + - ]: 2451 : Any* pStrippedArgs = aStrippedArguments.getArray();
174 : : Any* pStrippedArgsEnd = ::std::remove_copy_if(
175 : : _rArguments.getConstArray(),
176 : 2451 : _rArguments.getConstArray() + _rArguments.getLength(),
177 : : pStrippedArgs,
178 : : IsSpecialArgument()
179 [ + - ]: 2451 : );
180 [ + - ]: 2451 : aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
181 : :
182 [ - + ]: 2451 : if ( aStrippedArguments.getLength() )
183 : : {
184 [ # # ]: 0 : Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
185 : : OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
186 [ # # ]: 0 : if ( xModelInit.is() )
187 [ # # ][ # # ]: 0 : xModelInit->initialize( aStrippedArguments );
188 : : }
189 : :
190 [ + - ][ + - ]: 2451 : return xInstance;
191 : : }
192 : :
193 : : //--------------------------------------------------------------------
194 : 0 : ::rtl::OUString SAL_CALL SfxModelFactory::getImplementationName( ) throw (RuntimeException)
195 : : {
196 : 0 : return m_sImplementationName;
197 : : }
198 : :
199 : : //--------------------------------------------------------------------
200 : 0 : ::sal_Bool SAL_CALL SfxModelFactory::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
201 : : {
202 : : return ::std::find(
203 : : m_aServiceNames.getConstArray(),
204 : 0 : m_aServiceNames.getConstArray() + m_aServiceNames.getLength(),
205 : : _rServiceName
206 : 0 : ) != m_aServiceNames.getConstArray() + m_aServiceNames.getLength();
207 : : }
208 : :
209 : : //--------------------------------------------------------------------
210 : 0 : Sequence< ::rtl::OUString > SAL_CALL SfxModelFactory::getSupportedServiceNames( ) throw (RuntimeException)
211 : : {
212 : 0 : return m_aServiceNames;
213 : : }
214 : :
215 : : //--------------------------------------------------------------------
216 : 158 : Reference< XSingleServiceFactory > createSfxModelFactory( const Reference< XMultiServiceFactory >& _rxServiceFactory,
217 : : const ::rtl::OUString& _rImplementationName, const SfxModelFactoryFunc _pComponentFactoryFunc,
218 : : const Sequence< ::rtl::OUString >& _rServiceNames )
219 : : {
220 [ + - ][ + - ]: 158 : return new SfxModelFactory( _rxServiceFactory, _rImplementationName, _pComponentFactoryFunc, _rServiceNames );
221 : : }
222 : :
223 : : //........................................................................
224 : : } // namespace sfx2
225 : : //........................................................................
226 : :
227 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|