/src/libreoffice/framework/source/fwi/uielement/itemcontainer.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 <sal/config.h> |
21 | | |
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> |
27 | | |
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; |
33 | | |
34 | | constexpr OUString WRONG_TYPE_EXCEPTION |
35 | | = u"Type must be css::uno::Sequence< css::beans::PropertyValue >"_ustr; |
36 | | |
37 | | namespace framework |
38 | | { |
39 | | |
40 | | // XInterface, XTypeProvider |
41 | | |
42 | | ItemContainer::ItemContainer( const ShareableMutex& rMutex ) : |
43 | 0 | m_aShareMutex( rMutex ) |
44 | 0 | { |
45 | 0 | } |
46 | | |
47 | 0 | ItemContainer::ItemContainer( const ConstItemContainer& rConstItemContainer, const ShareableMutex& rMutex ) : m_aShareMutex( rMutex ) |
48 | 0 | { |
49 | 0 | copyItemContainer( rConstItemContainer.m_aItemVector, rMutex ); |
50 | 0 | } |
51 | | |
52 | | ItemContainer::ItemContainer( const Reference< XIndexAccess >& rSourceContainer, const ShareableMutex& rMutex ) : |
53 | 0 | m_aShareMutex( rMutex ) |
54 | 0 | { |
55 | 0 | if ( !rSourceContainer.is() ) |
56 | 0 | return; |
57 | | |
58 | 0 | sal_Int32 nCount = rSourceContainer->getCount(); |
59 | 0 | try |
60 | 0 | { |
61 | 0 | for ( sal_Int32 i = 0; i < nCount; i++ ) |
62 | 0 | { |
63 | 0 | Sequence< PropertyValue > aPropSeq; |
64 | 0 | if ( rSourceContainer->getByIndex( i ) >>= aPropSeq ) |
65 | 0 | { |
66 | 0 | sal_Int32 nContainerIndex = -1; |
67 | 0 | Reference< XIndexAccess > xIndexAccess; |
68 | 0 | for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ ) |
69 | 0 | { |
70 | 0 | if ( aPropSeq[j].Name == "ItemDescriptorContainer" ) |
71 | 0 | { |
72 | 0 | aPropSeq[j].Value >>= xIndexAccess; |
73 | 0 | nContainerIndex = j; |
74 | 0 | break; |
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | 0 | if ( xIndexAccess.is() && nContainerIndex >= 0 ) |
79 | 0 | aPropSeq.getArray()[nContainerIndex].Value <<= Reference<XIndexAccess>(deepCopyContainer( xIndexAccess, rMutex )); |
80 | |
|
81 | 0 | m_aItemVector.push_back( aPropSeq ); |
82 | 0 | } |
83 | 0 | } |
84 | 0 | } |
85 | 0 | catch ( const IndexOutOfBoundsException& ) |
86 | 0 | { |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | | ItemContainer::~ItemContainer() |
91 | 0 | { |
92 | 0 | } |
93 | | |
94 | | // private |
95 | | void ItemContainer::copyItemContainer( const std::vector< Sequence< PropertyValue > >& rSourceVector, const ShareableMutex& rMutex ) |
96 | 0 | { |
97 | 0 | const sal_uInt32 nCount = rSourceVector.size(); |
98 | 0 | for ( sal_uInt32 i = 0; i < nCount; ++i ) |
99 | 0 | { |
100 | 0 | sal_Int32 nContainerIndex = -1; |
101 | 0 | Sequence< PropertyValue > aPropSeq( rSourceVector[i] ); |
102 | 0 | Reference< XIndexAccess > xIndexAccess; |
103 | 0 | for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ ) |
104 | 0 | { |
105 | 0 | if ( aPropSeq[j].Name == "ItemDescriptorContainer" ) |
106 | 0 | { |
107 | 0 | aPropSeq[j].Value >>= xIndexAccess; |
108 | 0 | nContainerIndex = j; |
109 | 0 | break; |
110 | 0 | } |
111 | 0 | } |
112 | |
|
113 | 0 | if ( xIndexAccess.is() && nContainerIndex >= 0 ) |
114 | 0 | aPropSeq.getArray()[nContainerIndex].Value <<= Reference<XIndexAccess>(deepCopyContainer( xIndexAccess, rMutex )); |
115 | |
|
116 | 0 | m_aItemVector.push_back( aPropSeq ); |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | | rtl::Reference< ItemContainer > ItemContainer::deepCopyContainer( const Reference< XIndexAccess >& rSubContainer, const ShareableMutex& rMutex ) |
121 | 0 | { |
122 | 0 | rtl::Reference< ItemContainer > xReturn; |
123 | 0 | if ( rSubContainer.is() ) |
124 | 0 | { |
125 | 0 | ConstItemContainer* pSource = dynamic_cast<ConstItemContainer*>( rSubContainer.get() ); |
126 | 0 | if ( pSource ) |
127 | 0 | xReturn = new ItemContainer( *pSource, rMutex ); |
128 | 0 | else |
129 | 0 | xReturn = new ItemContainer( rSubContainer, rMutex ); |
130 | 0 | } |
131 | |
|
132 | 0 | return xReturn; |
133 | 0 | } |
134 | | |
135 | | // XElementAccess |
136 | | sal_Bool SAL_CALL ItemContainer::hasElements() |
137 | 0 | { |
138 | 0 | ShareGuard aLock( m_aShareMutex ); |
139 | 0 | return ( !m_aItemVector.empty() ); |
140 | 0 | } |
141 | | |
142 | | // XIndexAccess |
143 | | sal_Int32 SAL_CALL ItemContainer::getCount() |
144 | 0 | { |
145 | 0 | ShareGuard aLock( m_aShareMutex ); |
146 | 0 | return m_aItemVector.size(); |
147 | 0 | } |
148 | | |
149 | | Any SAL_CALL ItemContainer::getByIndex( sal_Int32 Index ) |
150 | 0 | { |
151 | 0 | ShareGuard aLock( m_aShareMutex ); |
152 | 0 | if ( sal_Int32( m_aItemVector.size()) <= Index ) |
153 | 0 | throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) ); |
154 | | |
155 | 0 | return Any( m_aItemVector[Index] ); |
156 | 0 | } |
157 | | |
158 | | // XIndexContainer |
159 | | void SAL_CALL ItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem ) |
160 | 0 | { |
161 | 0 | Sequence< PropertyValue > aSeq; |
162 | 0 | if ( !(aItem >>= aSeq) ) |
163 | 0 | throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, |
164 | 0 | static_cast<OWeakObject *>(this), 2 ); |
165 | | |
166 | 0 | ShareGuard aLock( m_aShareMutex ); |
167 | 0 | if ( sal_Int32( m_aItemVector.size()) == Index ) |
168 | 0 | m_aItemVector.push_back( aSeq ); |
169 | 0 | else if ( sal_Int32( m_aItemVector.size()) >Index ) |
170 | 0 | { |
171 | 0 | std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin(); |
172 | 0 | aIter += Index; |
173 | 0 | m_aItemVector.insert( aIter, aSeq ); |
174 | 0 | } |
175 | 0 | else |
176 | 0 | throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) ); |
177 | 0 | } |
178 | | |
179 | | void SAL_CALL ItemContainer::removeByIndex( sal_Int32 nIndex ) |
180 | 0 | { |
181 | 0 | ShareGuard aLock( m_aShareMutex ); |
182 | 0 | if ( static_cast<sal_Int32>(m_aItemVector.size()) <= nIndex ) |
183 | 0 | throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) ); |
184 | | |
185 | 0 | m_aItemVector.erase(m_aItemVector.begin() + nIndex); |
186 | 0 | } |
187 | | |
188 | | void SAL_CALL ItemContainer::replaceByIndex( sal_Int32 Index, const Any& aItem ) |
189 | 0 | { |
190 | 0 | Sequence< PropertyValue > aSeq; |
191 | 0 | if ( !(aItem >>= aSeq) ) |
192 | 0 | throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, |
193 | 0 | static_cast<OWeakObject *>(this), 2 ); |
194 | | |
195 | 0 | ShareGuard aLock( m_aShareMutex ); |
196 | 0 | if ( sal_Int32( m_aItemVector.size()) <= Index ) |
197 | 0 | throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) ); |
198 | | |
199 | 0 | m_aItemVector[Index] = std::move(aSeq); |
200 | 0 | } |
201 | | |
202 | | } // namespace framework |
203 | | |
204 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |