Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/doc/sfxmodelfactory.cxx
Line
Count
Source
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/beans/NamedValue.hpp>
23
#include <com/sun/star/lang/XInitialization.hpp>
24
25
#include <comphelper/namedvaluecollection.hxx>
26
27
#include <osl/diagnose.h>
28
29
#include <algorithm>
30
31
32
namespace 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
0
            {
53
0
                return  _rValueName == u"EmbeddedObject" || _rValueName == u"EmbeddedScriptSupport" || _rValueName == u"DocumentRecoverySupport";
54
0
            }
55
56
            bool operator()( const Any& _rArgument ) const
57
0
            {
58
0
                NamedValue aNamedValue;
59
0
                if ( ( _rArgument >>= aNamedValue ) && isSpecialArgumentName( aNamedValue.Name ) )
60
0
                    return true;
61
0
                PropertyValue aPropertyValue;
62
0
                return ( _rArgument >>= aPropertyValue ) && isSpecialArgumentName( aPropertyValue.Name );
63
0
            }
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
7.25k
    {
72
7.25k
        ::comphelper::NamedValueCollection aArgs( _rArguments );
73
7.25k
        const bool bEmbeddedObject = aArgs.getOrDefault( u"EmbeddedObject"_ustr, false );
74
7.25k
        const bool bScriptSupport = aArgs.getOrDefault( u"EmbeddedScriptSupport"_ustr, true );
75
7.25k
        const bool bDocRecoverySupport = aArgs.getOrDefault( u"DocumentRecoverySupport"_ustr, true );
76
77
7.25k
        SfxModelFlags nCreationFlags =
78
7.25k
                ( bEmbeddedObject ? SfxModelFlags::EMBEDDED_OBJECT : SfxModelFlags::NONE )
79
7.25k
            |   ( bScriptSupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS )
80
7.25k
            |   ( bDocRecoverySupport ? SfxModelFlags::NONE : SfxModelFlags::DISABLE_DOCUMENT_RECOVERY );
81
82
7.25k
        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
7.25k
        Sequence< Any > aStrippedArguments( _rArguments.getLength() );
87
7.25k
        Any* pStrippedArgs = aStrippedArguments.getArray();
88
7.25k
        Any* pStrippedArgsEnd = ::std::remove_copy_if(
89
7.25k
            _rArguments.begin(),
90
7.25k
            _rArguments.end(),
91
7.25k
            pStrippedArgs,
92
7.25k
            IsSpecialArgument()
93
7.25k
        );
94
7.25k
        aStrippedArguments.realloc( pStrippedArgsEnd - pStrippedArgs );
95
96
7.25k
        if ( aStrippedArguments.hasElements() )
97
0
        {
98
0
            Reference< XInitialization > xModelInit( xInstance, UNO_QUERY );
99
0
            OSL_ENSURE( xModelInit.is(), "SfxModelFactory::createInstanceWithArguments: no XInitialization!" );
100
0
            if ( xModelInit.is() )
101
0
                xModelInit->initialize( aStrippedArguments );
102
0
        }
103
104
7.25k
        return xInstance;
105
7.25k
    }
106
107
108
} // namespace sfx2
109
110
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */