LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / sfxmodelfactory.cxx
bloba14ff68b1a73fdd2170e1b79ee993bf0ef5924db
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/beans/NamedValue.hpp>
23 #include <com/sun/star/lang/XInitialization.hpp>
25 #include <comphelper/namedvaluecollection.hxx>
27 #include <osl/diagnose.h>
29 #include <algorithm>
32 namespace sfx2
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;
47 namespace
49 struct IsSpecialArgument
51 static bool isSpecialArgumentName( std::u16string_view _rValueName )
53 return _rValueName == u"EmbeddedObject" || _rValueName == u"EmbeddedScriptSupport" || _rValueName == u"DocumentRecoverySupport";
56 bool operator()( const Any& _rArgument ) const
58 NamedValue aNamedValue;
59 if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
60 return true;
61 PropertyValue aPropertyValue;
62 return ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name );
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)
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 );
77 SfxModelFlags nCreationFlags =
78 ( bEmbeddedObject ? SfxModelFlags::EMBEDDED_OBJECT : SfxModelFlags::NONE )
79 | ( bScriptSupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS )
80 | ( bDocRecoverySupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_DOCUMENT_RECOVERY );
82 Reference< XInterface > xInstance( creationFunc(nCreationFlags ) );
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()
94 aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
96 if ( aStrippedArguments.hasElements() )
98 Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
99 OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
100 if ( xModelInit.is() )
101 xModelInit->initialize( aStrippedArguments );
104 return xInstance;
108 } // namespace sfx2
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */