LibreOffice Module sfx2 (master) 1
sfxmodelfactory.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/beans/NamedValue.hpp>
23#include <com/sun/star/lang/XInitialization.hpp>
24
26
27#include <osl/diagnose.h>
28
29#include <algorithm>
30
31
32namespace sfx2
33{
34
35
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::XInterface;
38 using ::com::sun::star::uno::UNO_QUERY;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::Sequence;
41 using ::com::sun::star::beans::NamedValue;
42 using ::com::sun::star::beans::PropertyValue;
43 using ::com::sun::star::lang::XInitialization;
44
45
46
47 namespace
48 {
49 struct IsSpecialArgument
50 {
51 static bool isSpecialArgumentName( std::u16string_view _rValueName )
52 {
53 return _rValueName == u"EmbeddedObject" || _rValueName == u"EmbeddedScriptSupport" || _rValueName == u"DocumentRecoverySupport";
54 }
55
56 bool operator()( const Any& _rArgument ) const
57 {
58 NamedValue aNamedValue;
59 if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
60 return true;
61 PropertyValue aPropertyValue;
62 return ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name );
63 }
64 };
65 }
66
67
68 css::uno::Reference<css::uno::XInterface> createSfxModelInstance(
69 const css::uno::Sequence<css::uno::Any> & _rArguments,
70 std::function<css::uno::Reference<css::uno::XInterface>(SfxModelFlags)> creationFunc)
71 {
72 ::comphelper::NamedValueCollection aArgs( _rArguments );
73 const bool bEmbeddedObject = aArgs.getOrDefault( "EmbeddedObject", false );
74 const bool bScriptSupport = aArgs.getOrDefault( "EmbeddedScriptSupport", true );
75 const bool bDocRecoverySupport = aArgs.getOrDefault( "DocumentRecoverySupport", true );
76
77 SfxModelFlags nCreationFlags =
81
82 Reference< XInterface > xInstance( creationFunc(nCreationFlags ) );
83
84 // to mimic the behaviour of the default factory's createInstanceWithArguments, we initialize
85 // the object with the given arguments, stripped by the three special ones
86 Sequence< Any > aStrippedArguments( _rArguments.getLength() );
87 Any* pStrippedArgs = aStrippedArguments.getArray();
88 Any* pStrippedArgsEnd = ::std::remove_copy_if(
89 _rArguments.begin(),
90 _rArguments.end(),
91 pStrippedArgs,
92 IsSpecialArgument()
93 );
94 aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
95
96 if ( aStrippedArguments.hasElements() )
97 {
98 Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
99 OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
100 if ( xModelInit.is() )
101 xModelInit->initialize( aStrippedArguments );
102 }
103
104 return xInstance;
105 }
106
107
108} // namespace sfx2
109
110/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
VALUE_TYPE getOrDefault(const OUString &_rValueName, const VALUE_TYPE &_rDefault) const
float u
css::uno::Reference< css::uno::XInterface > createSfxModelInstance(const css::uno::Sequence< css::uno::Any > &_rArguments, std::function< css::uno::Reference< css::uno::XInterface >(SfxModelFlags)> creationFunc)
Intended to be called from UNO constructor functions This evaluates certain creation arguments (passe...
SfxModelFlags
@ DISABLE_DOCUMENT_RECOVERY
@ DISABLE_EMBEDDED_SCRIPTS