Line data Source code
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 <string.h>
21 :
22 : #include <comphelper/servicehelper.hxx>
23 : #include <comphelper/sequence.hxx>
24 : #include <uielement/rootitemcontainer.hxx>
25 : #include <uielement/itemcontainer.hxx>
26 : #include <uielement/constitemcontainer.hxx>
27 : #include <general.h>
28 : #include <properties.h>
29 :
30 : #include <com/sun/star/beans/PropertyAttribute.hpp>
31 :
32 : using namespace cppu;
33 : using namespace com::sun::star::uno;
34 : using namespace com::sun::star::lang;
35 : using namespace com::sun::star::beans;
36 : using namespace com::sun::star::container;
37 :
38 : const char WRONG_TYPE_EXCEPTION[] = "Type must be com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >";
39 :
40 : const int PROPHANDLE_UINAME = 1;
41 : const int PROPCOUNT = 1;
42 : const char PROPNAME_UINAME[] = "UIName";
43 :
44 : namespace framework
45 : {
46 :
47 430 : RootItemContainer::RootItemContainer()
48 : : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
49 430 : , ::cppu::OPropertySetHelper ( *(static_cast< ::cppu::OBroadcastHelper* >(this)) )
50 : {
51 430 : }
52 :
53 10 : RootItemContainer::RootItemContainer( const Reference< XIndexAccess >& rSourceContainer )
54 : : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
55 10 : , ::cppu::OPropertySetHelper ( *(static_cast< ::cppu::OBroadcastHelper* >(this)) )
56 : {
57 : // We also have to copy the UIName property
58 : try
59 : {
60 10 : Reference< XPropertySet > xPropSet( rSourceContainer, UNO_QUERY );
61 10 : if ( xPropSet.is() )
62 : {
63 10 : xPropSet->getPropertyValue("UIName") >>= m_aUIName;
64 10 : }
65 : }
66 0 : catch ( const Exception& )
67 : {
68 : }
69 :
70 10 : if ( rSourceContainer.is() )
71 : {
72 10 : sal_Int32 nCount = rSourceContainer->getCount();
73 : try
74 : {
75 102 : for ( sal_Int32 i = 0; i < nCount; i++ )
76 : {
77 92 : Sequence< PropertyValue > aPropSeq;
78 92 : if ( rSourceContainer->getByIndex( i ) >>= aPropSeq )
79 : {
80 92 : sal_Int32 nContainerIndex = -1;
81 92 : Reference< XIndexAccess > xIndexAccess;
82 278 : for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
83 : {
84 278 : if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
85 : {
86 92 : aPropSeq[j].Value >>= xIndexAccess;
87 92 : nContainerIndex = j;
88 92 : break;
89 : }
90 : }
91 :
92 92 : if ( xIndexAccess.is() && nContainerIndex >= 0 )
93 92 : aPropSeq[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess );
94 :
95 92 : m_aItemVector.push_back( aPropSeq );
96 : }
97 92 : }
98 : }
99 0 : catch ( const IndexOutOfBoundsException& )
100 : {
101 : }
102 : }
103 10 : }
104 :
105 880 : RootItemContainer::~RootItemContainer()
106 : {
107 880 : }
108 :
109 1452 : Any SAL_CALL RootItemContainer::queryInterface( const Type& _rType ) throw(RuntimeException, std::exception)
110 : {
111 1452 : Any aRet = RootItemContainer_BASE::queryInterface( _rType );
112 1452 : if ( !aRet.hasValue() )
113 436 : aRet = OPropertySetHelper::queryInterface( _rType );
114 1452 : return aRet;
115 : }
116 :
117 4 : Sequence< Type > SAL_CALL RootItemContainer::getTypes( ) throw(RuntimeException, std::exception)
118 : {
119 : return comphelper::concatSequences(
120 : RootItemContainer_BASE::getTypes(),
121 : ::cppu::OPropertySetHelper::getTypes()
122 4 : );
123 : }
124 :
125 92 : Reference< XIndexAccess > RootItemContainer::deepCopyContainer( const Reference< XIndexAccess >& rSubContainer )
126 : {
127 92 : Reference< XIndexAccess > xReturn;
128 92 : if ( rSubContainer.is() )
129 : {
130 92 : ConstItemContainer* pSource = ConstItemContainer::GetImplementation( rSubContainer );
131 92 : ItemContainer* pSubContainer( 0 );
132 92 : if ( pSource )
133 20 : pSubContainer = new ItemContainer( *pSource, m_aShareMutex );
134 : else
135 72 : pSubContainer = new ItemContainer( rSubContainer, m_aShareMutex );
136 92 : xReturn = Reference< XIndexAccess >( static_cast< OWeakObject* >( pSubContainer ), UNO_QUERY );
137 : }
138 :
139 92 : return xReturn;
140 : }
141 :
142 : // XUnoTunnel
143 428 : sal_Int64 RootItemContainer::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rIdentifier ) throw(::com::sun::star::uno::RuntimeException, std::exception)
144 : {
145 428 : if( ( rIdentifier.getLength() == 16 ) && ( 0 == memcmp( RootItemContainer::GetUnoTunnelId().getConstArray(), rIdentifier.getConstArray(), 16 ) ) )
146 428 : return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
147 0 : return 0;
148 : }
149 :
150 : namespace
151 : {
152 : class theRootItemContainerUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theRootItemContainerUnoTunnelId > {};
153 : }
154 :
155 856 : const Sequence< sal_Int8 >& RootItemContainer::GetUnoTunnelId() throw()
156 : {
157 856 : return theRootItemContainerUnoTunnelId::get().getSeq();
158 : }
159 :
160 428 : RootItemContainer* RootItemContainer::GetImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rxIFace ) throw()
161 : {
162 428 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xUT( rxIFace, ::com::sun::star::uno::UNO_QUERY );
163 428 : return xUT.is() ? reinterpret_cast< RootItemContainer* >(sal::static_int_cast< sal_IntPtr >(
164 428 : xUT->getSomething( RootItemContainer::GetUnoTunnelId() ))) : NULL;
165 : }
166 :
167 : // XElementAccess
168 0 : sal_Bool SAL_CALL RootItemContainer::hasElements()
169 : throw ( RuntimeException, std::exception )
170 : {
171 0 : ShareGuard aLock( m_aShareMutex );
172 0 : return ( !m_aItemVector.empty() );
173 : }
174 :
175 : // XIndexAccess
176 10288 : sal_Int32 SAL_CALL RootItemContainer::getCount()
177 : throw ( RuntimeException, std::exception )
178 : {
179 10288 : ShareGuard aLock( m_aShareMutex );
180 10288 : return m_aItemVector.size();
181 : }
182 :
183 9900 : Any SAL_CALL RootItemContainer::getByIndex( sal_Int32 Index )
184 : throw ( IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
185 : {
186 9900 : ShareGuard aLock( m_aShareMutex );
187 9900 : if ( sal_Int32( m_aItemVector.size()) > Index )
188 19800 : return makeAny( m_aItemVector[Index] );
189 : else
190 0 : throw IndexOutOfBoundsException( OUString(), (OWeakObject *)this );
191 : }
192 :
193 : // XIndexContainer
194 9786 : void SAL_CALL RootItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem )
195 : throw ( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
196 : {
197 9786 : Sequence< PropertyValue > aSeq;
198 9786 : if ( aItem >>= aSeq )
199 : {
200 9786 : ShareGuard aLock( m_aShareMutex );
201 9786 : if ( sal_Int32( m_aItemVector.size()) == Index )
202 9786 : m_aItemVector.push_back( aSeq );
203 0 : else if ( sal_Int32( m_aItemVector.size()) >Index )
204 : {
205 0 : std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin();
206 0 : aIter += Index;
207 0 : m_aItemVector.insert( aIter, aSeq );
208 : }
209 : else
210 0 : throw IndexOutOfBoundsException( OUString(), (OWeakObject *)this );
211 : }
212 : else
213 0 : throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, (OWeakObject *)this, 2 );
214 9786 : }
215 :
216 0 : void SAL_CALL RootItemContainer::removeByIndex( sal_Int32 nIndex )
217 : throw ( IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
218 : {
219 0 : ShareGuard aLock( m_aShareMutex );
220 0 : if ( (sal_Int32)m_aItemVector.size() > nIndex )
221 : {
222 0 : m_aItemVector.erase(m_aItemVector.begin() + nIndex);
223 : }
224 : else
225 0 : throw IndexOutOfBoundsException( OUString(), (OWeakObject *)this );
226 0 : }
227 :
228 0 : void SAL_CALL RootItemContainer::replaceByIndex( sal_Int32 Index, const Any& aItem )
229 : throw ( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
230 : {
231 0 : Sequence< PropertyValue > aSeq;
232 0 : if ( aItem >>= aSeq )
233 : {
234 0 : ShareGuard aLock( m_aShareMutex );
235 0 : if ( sal_Int32( m_aItemVector.size()) > Index )
236 0 : m_aItemVector[Index] = aSeq;
237 : else
238 0 : throw IndexOutOfBoundsException( OUString(), (OWeakObject *)this );
239 : }
240 : else
241 0 : throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, (OWeakObject *)this, 2 );
242 0 : }
243 :
244 3490 : Reference< XInterface > SAL_CALL RootItemContainer::createInstanceWithContext( const Reference< XComponentContext >& )
245 : throw ( Exception, RuntimeException, std::exception)
246 : {
247 3490 : return (OWeakObject *)(new ItemContainer( m_aShareMutex ));
248 : }
249 :
250 0 : Reference< XInterface > SAL_CALL RootItemContainer::createInstanceWithArgumentsAndContext( const Sequence< Any >&, const Reference< XComponentContext >& )
251 : throw (Exception, RuntimeException, std::exception)
252 : {
253 0 : return (OWeakObject *)(new ItemContainer( m_aShareMutex ));
254 : }
255 :
256 : // XPropertySet helper
257 0 : sal_Bool SAL_CALL RootItemContainer::convertFastPropertyValue( Any& aConvertedValue ,
258 : Any& aOldValue ,
259 : sal_Int32 nHandle ,
260 : const Any& aValue )
261 : throw( com::sun::star::lang::IllegalArgumentException )
262 : {
263 : // Initialize state with sal_False !!!
264 : // (Handle can be invalid)
265 0 : bool bReturn = false;
266 :
267 0 : switch( nHandle )
268 : {
269 : case PROPHANDLE_UINAME:
270 : bReturn = PropHelper::willPropertyBeChanged(
271 : com::sun::star::uno::makeAny(m_aUIName),
272 : aValue,
273 : aOldValue,
274 0 : aConvertedValue);
275 0 : break;
276 : }
277 :
278 : // Return state of operation.
279 0 : return bReturn;
280 : }
281 :
282 0 : void SAL_CALL RootItemContainer::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle ,
283 : const com::sun::star::uno::Any& aValue )
284 : throw( com::sun::star::uno::Exception, std::exception )
285 : {
286 0 : switch( nHandle )
287 : {
288 : case PROPHANDLE_UINAME:
289 0 : aValue >>= m_aUIName;
290 0 : break;
291 : }
292 0 : }
293 :
294 436 : void SAL_CALL RootItemContainer::getFastPropertyValue( com::sun::star::uno::Any& aValue ,
295 : sal_Int32 nHandle ) const
296 : {
297 436 : switch( nHandle )
298 : {
299 : case PROPHANDLE_UINAME:
300 436 : aValue <<= m_aUIName;
301 436 : break;
302 : }
303 436 : }
304 :
305 872 : ::cppu::IPropertyArrayHelper& SAL_CALL RootItemContainer::getInfoHelper()
306 : {
307 : // Optimize this method !
308 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
309 : // For the first call; pInfoHelper is NULL - for the second call pInfoHelper is different from NULL!
310 : static ::cppu::OPropertyArrayHelper* pInfoHelper = NULL;
311 :
312 872 : if( pInfoHelper == NULL )
313 : {
314 : // Ready for multithreading
315 66 : osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
316 :
317 : // Control this pointer again, another instance can be faster then these!
318 66 : if( pInfoHelper == NULL )
319 : {
320 : // Define static member to give structure of properties to baseclass "OPropertySetHelper".
321 : // "impl_getStaticPropertyDescriptor" is a non exported and static function, who will define a static propertytable.
322 : // "sal_True" say: Table is sorted by name.
323 66 : static ::cppu::OPropertyArrayHelper aInfoHelper( impl_getStaticPropertyDescriptor(), sal_True );
324 66 : pInfoHelper = &aInfoHelper;
325 66 : }
326 : }
327 :
328 872 : return(*pInfoHelper);
329 : }
330 :
331 0 : com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo > SAL_CALL RootItemContainer::getPropertySetInfo()
332 : throw (::com::sun::star::uno::RuntimeException, std::exception)
333 : {
334 : // Optimize this method !
335 : // We initialize a static variable only one time. And we don't must use a mutex at every call!
336 : // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
337 : static com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo >* pInfo = NULL;
338 :
339 0 : if( pInfo == NULL )
340 : {
341 : // Ready for multithreading
342 0 : osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
343 : // Control this pointer again, another instance can be faster then these!
344 0 : if( pInfo == NULL )
345 : {
346 : // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
347 : // (Use method "getInfoHelper()".)
348 0 : static com::sun::star::uno::Reference< com::sun::star::beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
349 0 : pInfo = &xInfo;
350 0 : }
351 : }
352 :
353 0 : return (*pInfo);
354 : }
355 :
356 66 : const com::sun::star::uno::Sequence< com::sun::star::beans::Property > RootItemContainer::impl_getStaticPropertyDescriptor()
357 : {
358 : // Create a property array to initialize sequence!
359 : // Table of all predefined properties of this class. Its used from OPropertySetHelper-class!
360 : // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
361 : // It's necessary for methods of OPropertySetHelper.
362 : // ATTENTION:
363 : // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
364 :
365 : const com::sun::star::beans::Property pProperties[] =
366 : {
367 : com::sun::star::beans::Property( OUString(PROPNAME_UINAME), PROPHANDLE_UINAME ,
368 66 : cppu::UnoType<OUString>::get(),
369 : com::sun::star::beans::PropertyAttribute::TRANSIENT )
370 198 : };
371 : // Use it to initialize sequence!
372 66 : const com::sun::star::uno::Sequence< com::sun::star::beans::Property > lPropertyDescriptor( pProperties, PROPCOUNT );
373 : // Return "PropertyDescriptor"
374 132 : return lPropertyDescriptor;
375 : }
376 :
377 : } // namespace framework
378 :
379 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|