Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/source/fwi/uielement/rootitemcontainer.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 <com/sun/star/lang/IndexOutOfBoundsException.hpp>
21
#include <comphelper/servicehelper.hxx>
22
#include <comphelper/sequence.hxx>
23
#include <uielement/rootitemcontainer.hxx>
24
#include <uielement/itemcontainer.hxx>
25
#include <uielement/constitemcontainer.hxx>
26
#include <properties.h>
27
28
#include <com/sun/star/beans/PropertyAttribute.hpp>
29
#include <rtl/ref.hxx>
30
31
using namespace cppu;
32
using namespace com::sun::star::uno;
33
using namespace com::sun::star::lang;
34
using namespace com::sun::star::beans;
35
using namespace com::sun::star::container;
36
37
constexpr OUString WRONG_TYPE_EXCEPTION
38
    = u"Type must be css::uno::Sequence< css::beans::PropertyValue >"_ustr;
39
40
const int PROPHANDLE_UINAME     = 1;
41
constexpr OUString PROPNAME_UINAME = u"UIName"_ustr;
42
43
namespace framework
44
{
45
46
RootItemContainer::RootItemContainer()
47
0
    :   ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
48
0
    ,   ::cppu::OPropertySetHelper  ( *static_cast< ::cppu::OBroadcastHelper* >(this) )
49
0
{
50
0
}
51
52
RootItemContainer::RootItemContainer( const Reference< XIndexAccess >& rSourceContainer )
53
0
    :   ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
54
0
    ,   ::cppu::OPropertySetHelper  ( *static_cast< ::cppu::OBroadcastHelper* >(this) )
55
0
{
56
    // We also have to copy the UIName property
57
0
    try
58
0
    {
59
0
        Reference< XPropertySet > xPropSet( rSourceContainer, UNO_QUERY );
60
0
        if ( xPropSet.is() )
61
0
        {
62
0
            xPropSet->getPropertyValue(u"UIName"_ustr) >>= m_aUIName;
63
0
        }
64
0
    }
65
0
    catch ( const Exception& )
66
0
    {
67
0
    }
68
69
0
    if ( !rSourceContainer.is() )
70
0
        return;
71
72
0
    sal_Int32 nCount = rSourceContainer->getCount();
73
0
    try
74
0
    {
75
0
        for ( sal_Int32 i = 0; i < nCount; i++ )
76
0
        {
77
0
            Sequence< PropertyValue > aPropSeq;
78
0
            if ( rSourceContainer->getByIndex( i ) >>= aPropSeq )
79
0
            {
80
0
                sal_Int32 nContainerIndex = -1;
81
0
                Reference< XIndexAccess > xIndexAccess;
82
0
                for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
83
0
                {
84
0
                    if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
85
0
                    {
86
0
                        aPropSeq[j].Value >>= xIndexAccess;
87
0
                        nContainerIndex = j;
88
0
                        break;
89
0
                    }
90
0
                }
91
92
0
                if ( xIndexAccess.is() && nContainerIndex >= 0 )
93
0
                    aPropSeq.getArray()[nContainerIndex].Value <<= Reference<XIndexAccess>(deepCopyContainer( xIndexAccess ));
94
95
0
                m_aItemVector.push_back( aPropSeq );
96
0
            }
97
0
        }
98
0
    }
99
0
    catch ( const IndexOutOfBoundsException& )
100
0
    {
101
0
    }
102
0
}
103
104
RootItemContainer::~RootItemContainer()
105
0
{
106
0
}
107
108
Any SAL_CALL RootItemContainer::queryInterface( const Type& _rType )
109
0
{
110
0
    Any aRet = RootItemContainer_BASE::queryInterface( _rType );
111
0
    if ( !aRet.hasValue() )
112
0
        aRet = OPropertySetHelper::queryInterface( _rType );
113
0
    return aRet;
114
0
}
115
116
Sequence< Type > SAL_CALL RootItemContainer::getTypes(  )
117
0
{
118
0
    return comphelper::concatSequences(
119
0
        RootItemContainer_BASE::getTypes(),
120
0
        ::cppu::OPropertySetHelper::getTypes()
121
0
    );
122
0
}
123
124
rtl::Reference< ItemContainer > RootItemContainer::deepCopyContainer( const Reference< XIndexAccess >& rSubContainer )
125
0
{
126
0
    rtl::Reference< ItemContainer > xReturn;
127
0
    if ( rSubContainer.is() )
128
0
    {
129
0
        ConstItemContainer* pSource = dynamic_cast<ConstItemContainer*>( rSubContainer.get() );
130
0
        if ( pSource )
131
0
            xReturn = new ItemContainer( *pSource, m_aShareMutex );
132
0
        else
133
0
            xReturn = new ItemContainer( rSubContainer, m_aShareMutex );
134
0
    }
135
136
0
    return xReturn;
137
0
}
138
139
// XElementAccess
140
sal_Bool SAL_CALL RootItemContainer::hasElements()
141
0
{
142
0
    ShareGuard aLock( m_aShareMutex );
143
0
    return ( !m_aItemVector.empty() );
144
0
}
145
146
// XIndexAccess
147
sal_Int32 SAL_CALL RootItemContainer::getCount()
148
0
{
149
0
    ShareGuard aLock( m_aShareMutex );
150
0
    return m_aItemVector.size();
151
0
}
152
153
Any SAL_CALL RootItemContainer::getByIndex( sal_Int32 Index )
154
0
{
155
0
    ShareGuard aLock( m_aShareMutex );
156
0
    if ( sal_Int32( m_aItemVector.size()) <= Index )
157
0
        throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
158
159
0
    return Any( m_aItemVector[Index] );
160
0
}
161
162
// XIndexContainer
163
void SAL_CALL RootItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem )
164
0
{
165
0
    Sequence< PropertyValue > aSeq;
166
0
    if ( !(aItem >>= aSeq) )
167
0
        throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, static_cast<OWeakObject *>(this), 2 );
168
169
0
    ShareGuard aLock( m_aShareMutex );
170
0
    if ( sal_Int32( m_aItemVector.size()) == Index )
171
0
        m_aItemVector.push_back( aSeq );
172
0
    else if ( sal_Int32( m_aItemVector.size()) >Index )
173
0
    {
174
0
        std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin();
175
0
        aIter += Index;
176
0
        m_aItemVector.insert( aIter, aSeq );
177
0
    }
178
0
    else
179
0
        throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
180
0
}
181
182
void SAL_CALL RootItemContainer::removeByIndex( sal_Int32 nIndex )
183
0
{
184
0
    ShareGuard aLock( m_aShareMutex );
185
0
    if ( static_cast<sal_Int32>(m_aItemVector.size()) <= nIndex )
186
0
        throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
187
188
0
    m_aItemVector.erase(m_aItemVector.begin() + nIndex);
189
0
}
190
191
void SAL_CALL RootItemContainer::replaceByIndex( sal_Int32 Index, const Any& aItem )
192
0
{
193
0
    Sequence< PropertyValue > aSeq;
194
0
    if ( !(aItem >>= aSeq) )
195
0
        throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, static_cast<OWeakObject *>(this), 2 );
196
197
0
    ShareGuard aLock( m_aShareMutex );
198
0
    if ( sal_Int32( m_aItemVector.size()) <= Index )
199
0
        throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
200
201
0
    m_aItemVector[Index] = std::move(aSeq);
202
0
}
203
204
Reference< XInterface > SAL_CALL RootItemContainer::createInstanceWithContext( const Reference< XComponentContext >& )
205
0
{
206
0
    return static_cast<OWeakObject *>(new ItemContainer( m_aShareMutex ));
207
0
}
208
209
Reference< XInterface > SAL_CALL RootItemContainer::createInstanceWithArgumentsAndContext( const Sequence< Any >&, const Reference< XComponentContext >& )
210
0
{
211
0
    return static_cast<OWeakObject *>(new ItemContainer( m_aShareMutex ));
212
0
}
213
214
// XPropertySet helper
215
sal_Bool SAL_CALL RootItemContainer::convertFastPropertyValue( Any&       aConvertedValue ,
216
                                                               Any&       aOldValue       ,
217
                                                               sal_Int32  nHandle         ,
218
                                                               const Any& aValue             )
219
0
{
220
    //  Initialize state with sal_False !!!
221
    //  (Handle can be invalid)
222
0
    bool bReturn = false;
223
224
0
    switch( nHandle )
225
0
    {
226
0
        case PROPHANDLE_UINAME:
227
0
            bReturn = PropHelper::willPropertyBeChanged(
228
0
                        css::uno::Any(m_aUIName),
229
0
                        aValue,
230
0
                        aOldValue,
231
0
                        aConvertedValue);
232
0
            break;
233
0
    }
234
235
    // Return state of operation.
236
0
    return bReturn;
237
0
}
238
239
void SAL_CALL RootItemContainer::setFastPropertyValue_NoBroadcast( sal_Int32               nHandle ,
240
                                                                   const css::uno::Any&    aValue  )
241
0
{
242
0
    switch( nHandle )
243
0
    {
244
0
        case PROPHANDLE_UINAME:
245
0
            aValue >>= m_aUIName;
246
0
            break;
247
0
    }
248
0
}
249
250
void SAL_CALL RootItemContainer::getFastPropertyValue( css::uno::Any& aValue  ,
251
                                                       sal_Int32                 nHandle                ) const
252
0
{
253
0
    switch( nHandle )
254
0
    {
255
0
        case PROPHANDLE_UINAME:
256
0
            aValue <<= m_aUIName;
257
0
            break;
258
0
    }
259
0
}
260
261
::cppu::IPropertyArrayHelper& SAL_CALL RootItemContainer::getInfoHelper()
262
0
{
263
    // Define static member to give structure of properties to baseclass "OPropertySetHelper".
264
    // "impl_getStaticPropertyDescriptor" is a non exported and static function, who will define a static propertytable.
265
    // "true" say: Table is sorted by name.
266
0
    static ::cppu::OPropertyArrayHelper ourInfoHelper( impl_getStaticPropertyDescriptor(), true );
267
268
0
    return ourInfoHelper;
269
0
}
270
271
css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL RootItemContainer::getPropertySetInfo()
272
0
{
273
    // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
274
    // (Use method "getInfoHelper()".)
275
0
    static css::uno::Reference< css::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
276
277
0
    return xInfo;
278
0
}
279
280
css::uno::Sequence< css::beans::Property > RootItemContainer::impl_getStaticPropertyDescriptor()
281
0
{
282
    // Create a property array to initialize sequence!
283
    // Table of all predefined properties of this class. It's used from OPropertySetHelper-class!
284
    // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
285
    // It's necessary for methods of OPropertySetHelper.
286
    // ATTENTION:
287
    //      YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
288
289
0
    return
290
0
    {
291
0
        css::beans::Property( PROPNAME_UINAME, PROPHANDLE_UINAME ,
292
0
                              cppu::UnoType<OUString>::get(),
293
0
                              css::beans::PropertyAttribute::TRANSIENT )
294
0
    };
295
0
}
296
297
} // namespace framework
298
299
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */