Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / doc / sfxmodelfactory.cxx
blob556b3f00f5bf19a374cea1a1f8a7f6fb9c01a5c4
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>
25 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <comphelper/namedvaluecollection.hxx>
30 #include <cppuhelper/implbase.hxx>
31 #include <cppuhelper/supportsservice.hxx>
33 #include <osl/diagnose.h>
35 #include <algorithm>
38 namespace sfx2
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::uno::XInterface;
44 using ::com::sun::star::uno::UNO_QUERY;
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;
56 namespace
58 struct IsSpecialArgument
60 static bool isSpecialArgumentName( const OUString& _rValueName )
62 return _rValueName == "EmbeddedObject" || _rValueName == "EmbeddedScriptSupport" || _rValueName == "DocumentRecoverySupport";
65 bool operator()( const Any& _rArgument ) const
67 NamedValue aNamedValue;
68 if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
69 return true;
70 PropertyValue aPropertyValue;
71 return ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name );
77 css::uno::Reference<css::uno::XInterface> createSfxModelInstance(
78 const css::uno::Sequence<css::uno::Any> & _rArguments,
79 std::function<css::uno::Reference<css::uno::XInterface>(SfxModelFlags)> creationFunc)
81 ::comphelper::NamedValueCollection aArgs( _rArguments );
82 const bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", false );
83 const bool bScriptSupport = aArgs.getOrDefault( "EmbeddedScriptSupport", true );
84 const bool bDocRecoverySupport = aArgs.getOrDefault( "DocumentRecoverySupport", true );
86 SfxModelFlags nCreationFlags =
87 ( bEmbeddedObject ? SfxModelFlags::EMBEDDED_OBJECT : SfxModelFlags::NONE )
88 | ( bScriptSupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS )
89 | ( bDocRecoverySupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_DOCUMENT_RECOVERY );
91 Reference< XInterface > xInstance( creationFunc(nCreationFlags ) );
93 // to mimic the behaviour of the default factory's createInstanceWithArguments, we initialize
94 // the object with the given arguments, stripped by the three special ones
95 Sequence< Any > aStrippedArguments( _rArguments.getLength() );
96 Any* pStrippedArgs = aStrippedArguments.getArray();
97 Any* pStrippedArgsEnd = ::std::remove_copy_if(
98 _rArguments.begin(),
99 _rArguments.end(),
100 pStrippedArgs,
101 IsSpecialArgument()
103 aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
105 if ( aStrippedArguments.hasElements() )
107 Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
108 OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
109 if ( xModelInit.is() )
110 xModelInit->initialize( aStrippedArguments );
113 return xInstance;
117 } // namespace sfx2
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */