1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/implbase2.hxx>
32 //........................................................................
35 //........................................................................
37 using ::com::sun::star::uno::Reference
;
38 using ::com::sun::star::uno::XInterface
;
39 using ::com::sun::star::uno::UNO_QUERY
;
40 using ::com::sun::star::uno::UNO_QUERY_THROW
;
41 using ::com::sun::star::uno::UNO_SET_THROW
;
42 using ::com::sun::star::uno::Exception
;
43 using ::com::sun::star::uno::RuntimeException
;
44 using ::com::sun::star::uno::Any
;
45 using ::com::sun::star::uno::makeAny
;
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
;
54 //====================================================================
55 //= SfxModelFactory - declaration
56 //====================================================================
57 typedef ::cppu::WeakImplHelper2
< XSingleServiceFactory
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
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
);
79 virtual Reference
< XInterface
> SAL_CALL
createInstanceWithArguments( const Sequence
< Any
>& aArguments
) throw (Exception
, RuntimeException
);
82 virtual OUString SAL_CALL
getImplementationName( ) throw (RuntimeException
);
83 virtual ::sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw (RuntimeException
);
84 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) throw (RuntimeException
);
87 virtual ~SfxModelFactory();
90 Reference
< XInterface
> impl_createInstance( const sal_uInt64 _nCreationFlags
) const;
93 const Reference
< XMultiServiceFactory
> m_xServiceFactory
;
94 const OUString m_sImplementationName
;
95 const Sequence
< OUString
> m_aServiceNames
;
96 const SfxModelFactoryFunc m_pComponentFactoryFunc
;
99 //====================================================================
100 //= SfxModelFactory - implementation
101 //====================================================================
102 //--------------------------------------------------------------------
103 SfxModelFactory::SfxModelFactory( const Reference
< XMultiServiceFactory
>& _rxServiceFactory
,
104 const OUString
& _rImplementationName
, const SfxModelFactoryFunc _pComponentFactoryFunc
,
105 const Sequence
< OUString
>& _rServiceNames
)
106 :m_xServiceFactory( _rxServiceFactory
)
107 ,m_sImplementationName( _rImplementationName
)
108 ,m_aServiceNames( _rServiceNames
)
109 ,m_pComponentFactoryFunc( _pComponentFactoryFunc
)
113 //--------------------------------------------------------------------
114 SfxModelFactory::~SfxModelFactory()
118 //--------------------------------------------------------------------
119 Reference
< XInterface
> SfxModelFactory::impl_createInstance( const sal_uInt64 _nCreationFlags
) const
121 return (*m_pComponentFactoryFunc
)( m_xServiceFactory
, _nCreationFlags
);
124 //--------------------------------------------------------------------
125 Reference
< XInterface
> SAL_CALL
SfxModelFactory::createInstance( ) throw (Exception
, RuntimeException
)
127 return createInstanceWithArguments( Sequence
< Any
>() );
130 //--------------------------------------------------------------------
133 struct IsSpecialArgument
: public ::std::unary_function
< Any
, bool >
135 static bool isSpecialArgumentName( const OUString
& _rValueName
)
137 return _rValueName
== "EmbeddedObject" || _rValueName
== "EmbeddedScriptSupport" || _rValueName
== "DocumentRecoverySupport";
140 bool operator()( const Any
& _rArgument
) const
142 NamedValue aNamedValue
;
143 if ( ( _rArgument
>>= aNamedValue
) && isSpecialArgumentName( aNamedValue
.Name
) )
145 PropertyValue aPropertyValue
;
146 if ( ( _rArgument
>>= aPropertyValue
) && isSpecialArgumentName( aPropertyValue
.Name
) )
153 //--------------------------------------------------------------------
154 Reference
< XInterface
> SAL_CALL
SfxModelFactory::createInstanceWithArguments( const Sequence
< Any
>& _rArguments
) throw (Exception
, RuntimeException
)
156 ::comphelper::NamedValueCollection
aArgs( _rArguments
);
157 const sal_Bool bEmbeddedObject
= aArgs
.getOrDefault( "EmbeddedObject", sal_False
);
158 const sal_Bool bScriptSupport
= aArgs
.getOrDefault( "EmbeddedScriptSupport", sal_True
);
159 const sal_Bool bDocRecoverySupport
= aArgs
.getOrDefault( "DocumentRecoverySupport", sal_True
);
161 sal_uInt64 nCreationFlags
=
162 ( bEmbeddedObject
? SFXMODEL_EMBEDDED_OBJECT
: 0 )
163 | ( bScriptSupport
? 0 : SFXMODEL_DISABLE_EMBEDDED_SCRIPTS
)
164 | ( bDocRecoverySupport
? 0 : SFXMODEL_DISABLE_DOCUMENT_RECOVERY
);
166 Reference
< XInterface
> xInstance( impl_createInstance( nCreationFlags
) );
168 // to mimic the bahaviour of the default factory's createInstanceWithArguments, we initialize
169 // the object with the given arguments, stripped by the three special ones
170 Sequence
< Any
> aStrippedArguments( _rArguments
.getLength() );
171 Any
* pStrippedArgs
= aStrippedArguments
.getArray();
172 Any
* pStrippedArgsEnd
= ::std::remove_copy_if(
173 _rArguments
.getConstArray(),
174 _rArguments
.getConstArray() + _rArguments
.getLength(),
178 aStrippedArguments
.realloc( pStrippedArgsEnd
- pStrippedArgs
);
180 if ( aStrippedArguments
.getLength() )
182 Reference
< XInitialization
> xModelInit( xInstance
, UNO_QUERY
);
183 OSL_ENSURE( xModelInit
.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
184 if ( xModelInit
.is() )
185 xModelInit
->initialize( aStrippedArguments
);
191 //--------------------------------------------------------------------
192 OUString SAL_CALL
SfxModelFactory::getImplementationName( ) throw (RuntimeException
)
194 return m_sImplementationName
;
197 //--------------------------------------------------------------------
198 ::sal_Bool SAL_CALL
SfxModelFactory::supportsService( const OUString
& _rServiceName
) throw (RuntimeException
)
201 m_aServiceNames
.getConstArray(),
202 m_aServiceNames
.getConstArray() + m_aServiceNames
.getLength(),
204 ) != m_aServiceNames
.getConstArray() + m_aServiceNames
.getLength();
207 //--------------------------------------------------------------------
208 Sequence
< OUString
> SAL_CALL
SfxModelFactory::getSupportedServiceNames( ) throw (RuntimeException
)
210 return m_aServiceNames
;
213 //--------------------------------------------------------------------
214 Reference
< XSingleServiceFactory
> createSfxModelFactory( const Reference
< XMultiServiceFactory
>& _rxServiceFactory
,
215 const OUString
& _rImplementationName
, const SfxModelFactoryFunc _pComponentFactoryFunc
,
216 const Sequence
< OUString
>& _rServiceNames
)
218 return new SfxModelFactory( _rxServiceFactory
, _rImplementationName
, _pComponentFactoryFunc
, _rServiceNames
);
221 //........................................................................
223 //........................................................................
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */