Update git submodules
[LibreOffice.git] / framework / source / fwi / uielement / itemcontainer.cxx
blobec70eaad3ae2d3907fed8d219e9b862622dee58e
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 <sal/config.h>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <uielement/itemcontainer.hxx>
24 #include <uielement/constitemcontainer.hxx>
25 #include <comphelper/servicehelper.hxx>
26 #include <rtl/ref.hxx>
28 using namespace cppu;
29 using namespace com::sun::star::uno;
30 using namespace com::sun::star::lang;
31 using namespace com::sun::star::beans;
32 using namespace com::sun::star::container;
34 constexpr OUString WRONG_TYPE_EXCEPTION
35 = u"Type must be css::uno::Sequence< css::beans::PropertyValue >"_ustr;
37 namespace framework
40 // XInterface, XTypeProvider
42 ItemContainer::ItemContainer( const ShareableMutex& rMutex ) :
43 m_aShareMutex( rMutex )
47 ItemContainer::ItemContainer( const ConstItemContainer& rConstItemContainer, const ShareableMutex& rMutex ) : m_aShareMutex( rMutex )
49 copyItemContainer( rConstItemContainer.m_aItemVector, rMutex );
52 ItemContainer::ItemContainer( const Reference< XIndexAccess >& rSourceContainer, const ShareableMutex& rMutex ) :
53 m_aShareMutex( rMutex )
55 if ( !rSourceContainer.is() )
56 return;
58 sal_Int32 nCount = rSourceContainer->getCount();
59 try
61 for ( sal_Int32 i = 0; i < nCount; i++ )
63 Sequence< PropertyValue > aPropSeq;
64 if ( rSourceContainer->getByIndex( i ) >>= aPropSeq )
66 sal_Int32 nContainerIndex = -1;
67 Reference< XIndexAccess > xIndexAccess;
68 for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
70 if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
72 aPropSeq[j].Value >>= xIndexAccess;
73 nContainerIndex = j;
74 break;
78 if ( xIndexAccess.is() && nContainerIndex >= 0 )
79 aPropSeq.getArray()[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess, rMutex );
81 m_aItemVector.push_back( aPropSeq );
85 catch ( const IndexOutOfBoundsException& )
90 ItemContainer::~ItemContainer()
94 // private
95 void ItemContainer::copyItemContainer( const std::vector< Sequence< PropertyValue > >& rSourceVector, const ShareableMutex& rMutex )
97 const sal_uInt32 nCount = rSourceVector.size();
98 for ( sal_uInt32 i = 0; i < nCount; ++i )
100 sal_Int32 nContainerIndex = -1;
101 Sequence< PropertyValue > aPropSeq( rSourceVector[i] );
102 Reference< XIndexAccess > xIndexAccess;
103 for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
105 if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
107 aPropSeq[j].Value >>= xIndexAccess;
108 nContainerIndex = j;
109 break;
113 if ( xIndexAccess.is() && nContainerIndex >= 0 )
114 aPropSeq.getArray()[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess, rMutex );
116 m_aItemVector.push_back( aPropSeq );
120 Reference< XIndexAccess > ItemContainer::deepCopyContainer( const Reference< XIndexAccess >& rSubContainer, const ShareableMutex& rMutex )
122 Reference< XIndexAccess > xReturn;
123 if ( rSubContainer.is() )
125 ConstItemContainer* pSource = dynamic_cast<ConstItemContainer*>( rSubContainer.get() );
126 rtl::Reference<ItemContainer> pSubContainer;
127 if ( pSource )
128 pSubContainer = new ItemContainer( *pSource, rMutex );
129 else
130 pSubContainer = new ItemContainer( rSubContainer, rMutex );
131 xReturn = pSubContainer;
134 return xReturn;
137 // XElementAccess
138 sal_Bool SAL_CALL ItemContainer::hasElements()
140 ShareGuard aLock( m_aShareMutex );
141 return ( !m_aItemVector.empty() );
144 // XIndexAccess
145 sal_Int32 SAL_CALL ItemContainer::getCount()
147 ShareGuard aLock( m_aShareMutex );
148 return m_aItemVector.size();
151 Any SAL_CALL ItemContainer::getByIndex( sal_Int32 Index )
153 ShareGuard aLock( m_aShareMutex );
154 if ( sal_Int32( m_aItemVector.size()) <= Index )
155 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
157 return Any( m_aItemVector[Index] );
160 // XIndexContainer
161 void SAL_CALL ItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem )
163 Sequence< PropertyValue > aSeq;
164 if ( !(aItem >>= aSeq) )
165 throw IllegalArgumentException( WRONG_TYPE_EXCEPTION,
166 static_cast<OWeakObject *>(this), 2 );
168 ShareGuard aLock( m_aShareMutex );
169 if ( sal_Int32( m_aItemVector.size()) == Index )
170 m_aItemVector.push_back( aSeq );
171 else if ( sal_Int32( m_aItemVector.size()) >Index )
173 std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin();
174 aIter += Index;
175 m_aItemVector.insert( aIter, aSeq );
177 else
178 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
181 void SAL_CALL ItemContainer::removeByIndex( sal_Int32 nIndex )
183 ShareGuard aLock( m_aShareMutex );
184 if ( static_cast<sal_Int32>(m_aItemVector.size()) <= nIndex )
185 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
187 m_aItemVector.erase(m_aItemVector.begin() + nIndex);
190 void SAL_CALL ItemContainer::replaceByIndex( sal_Int32 Index, const Any& aItem )
192 Sequence< PropertyValue > aSeq;
193 if ( !(aItem >>= aSeq) )
194 throw IllegalArgumentException( WRONG_TYPE_EXCEPTION,
195 static_cast<OWeakObject *>(this), 2 );
197 ShareGuard aLock( m_aShareMutex );
198 if ( sal_Int32( m_aItemVector.size()) <= Index )
199 throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
201 m_aItemVector[Index] = std::move(aSeq);
204 } // namespace framework
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */