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 245 : RootItemContainer::RootItemContainer()
48 : : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
49 245 : , ::cppu::OPropertySetHelper ( *(static_cast< ::cppu::OBroadcastHelper* >(this)) )
50 : {
51 245 : }
52 :
53 5 : RootItemContainer::RootItemContainer( const Reference< XIndexAccess >& rSourceContainer )
54 : : ::cppu::OBroadcastHelperVar< ::cppu::OMultiTypeInterfaceContainerHelper, ::cppu::OMultiTypeInterfaceContainerHelper::keyType >( m_aMutex )
55 5 : , ::cppu::OPropertySetHelper ( *(static_cast< ::cppu::OBroadcastHelper* >(this)) )
56 : {
57 : // We also have to copy the UIName property
58 : try
59 : {
60 5 : Reference< XPropertySet > xPropSet( rSourceContainer, UNO_QUERY );
61 5 : if ( xPropSet.is() )
62 : {
63 5 : xPropSet->getPropertyValue("UIName") >>= m_aUIName;
64 5 : }
65 : }
66 0 : catch ( const Exception& )
67 : {
68 : }
69 :
70 5 : if ( rSourceContainer.is() )
71 : {
72 5 : sal_Int32 nCount = rSourceContainer->getCount();
73 : try
74 : {
75 51 : for ( sal_Int32 i = 0; i < nCount; i++ )
76 : {
77 46 : Sequence< PropertyValue > aPropSeq;
78 46 : if ( rSourceContainer->getByIndex( i ) >>= aPropSeq )
79 : {
80 46 : sal_Int32 nContainerIndex = -1;
81 46 : Reference< XIndexAccess > xIndexAccess;
82 139 : for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
83 : {
84 139 : if ( aPropSeq[j].Name == "ItemDescriptorContainer" )
85 : {
86 46 : aPropSeq[j].Value >>= xIndexAccess;
87 46 : nContainerIndex = j;
88 46 : break;
89 : }
90 : }
91 :
92 46 : if ( xIndexAccess.is() && nContainerIndex >= 0 )
93 46 : aPropSeq[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess );
94 :
95 46 : m_aItemVector.push_back( aPropSeq );
96 : }
97 46 : }
98 : }
99 0 : catch ( const IndexOutOfBoundsException& )
100 : {
101 : }
102 : }
103 5 : }
104 :
105 500 : RootItemContainer::~RootItemContainer()
106 : {
107 500 : }
108 :
109 826 : Any SAL_CALL RootItemContainer::queryInterface( const Type& _rType ) throw(RuntimeException, std::exception)
110 : {
111 826 : Any aRet = RootItemContainer_BASE::queryInterface( _rType );
112 826 : if ( !aRet.hasValue() )
113 248 : aRet = OPropertySetHelper::queryInterface( _rType );
114 826 : return aRet;
115 : }
116 :
117 2 : Sequence< Type > SAL_CALL RootItemContainer::getTypes( ) throw(RuntimeException, std::exception)
118 : {
119 : return comphelper::concatSequences(
120 : RootItemContainer_BASE::getTypes(),
121 : ::cppu::OPropertySetHelper::getTypes()
122 2 : );
123 : }
124 :
125 46 : Reference< XIndexAccess > RootItemContainer::deepCopyContainer( const Reference< XIndexAccess >& rSubContainer )
126 : {
127 46 : Reference< XIndexAccess > xReturn;
128 46 : if ( rSubContainer.is() )
129 : {
130 46 : ConstItemContainer* pSource = ConstItemContainer::GetImplementation( rSubContainer );
131 46 : ItemContainer* pSubContainer( 0 );
132 46 : if ( pSource )
133 10 : pSubContainer = new ItemContainer( *pSource, m_aShareMutex );
134 : else
135 36 : pSubContainer = new ItemContainer( rSubContainer, m_aShareMutex );
136 46 : xReturn = Reference< XIndexAccess >( static_cast< OWeakObject* >( pSubContainer ), UNO_QUERY );
137 : }
138 :
139 46 : return xReturn;
140 : }
141 :
142 : // XUnoTunnel
143 244 : sal_Int64 RootItemContainer::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rIdentifier ) throw(::com::sun::star::uno::RuntimeException, std::exception)
144 : {
145 244 : if( ( rIdentifier.getLength() == 16 ) && ( 0 == memcmp( RootItemContainer::GetUnoTunnelId().getConstArray(), rIdentifier.getConstArray(), 16 ) ) )
146 244 : 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 488 : const Sequence< sal_Int8 >& RootItemContainer::GetUnoTunnelId() throw()
156 : {
157 488 : return theRootItemContainerUnoTunnelId::get().getSeq();
158 : }
159 :
160 244 : RootItemContainer* RootItemContainer::GetImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rxIFace ) throw()
161 : {
162 244 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xUT( rxIFace, ::com::sun::star::uno::UNO_QUERY );
163 244 : return xUT.is() ? reinterpret_cast< RootItemContainer* >(sal::static_int_cast< sal_IntPtr >(
164 244 : 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 7855 : sal_Int32 SAL_CALL RootItemContainer::getCount()
177 : throw ( RuntimeException, std::exception )
178 : {
179 7855 : ShareGuard aLock( m_aShareMutex );
180 7855 : return m_aItemVector.size();
181 : }
182 :
183 7631 : Any SAL_CALL RootItemContainer::getByIndex( sal_Int32 Index )
184 : throw ( IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
185 : {
186 7631 : ShareGuard aLock( m_aShareMutex );
187 7631 : if ( sal_Int32( m_aItemVector.size()) > Index )
188 15262 : return makeAny( m_aItemVector[Index] );
189 : else
190 0 : throw IndexOutOfBoundsException( OUString(), static_cast<OWeakObject *>(this) );
191 : }
192 :
193 : // XIndexContainer
194 7574 : void SAL_CALL RootItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem )
195 : throw ( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException, std::exception )
196 : {
197 7574 : Sequence< PropertyValue > aSeq;
198 7574 : if ( aItem >>= aSeq )
199 : {
200 7574 : ShareGuard aLock( m_aShareMutex );
201 7574 : if ( sal_Int32( m_aItemVector.size()) == Index )
202 7574 : 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(), static_cast<OWeakObject *>(this) );
211 : }
212 : else
213 0 : throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, static_cast<OWeakObject *>(this), 2 );
214 7574 : }
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(), static_cast<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(), static_cast<OWeakObject *>(this) );
239 : }
240 : else
241 0 : throw IllegalArgumentException( WRONG_TYPE_EXCEPTION, static_cast<OWeakObject *>(this), 2 );
242 0 : }
243 :
244 2724 : Reference< XInterface > SAL_CALL RootItemContainer::createInstanceWithContext( const Reference< XComponentContext >& )
245 : throw ( Exception, RuntimeException, std::exception)
246 : {
247 2724 : return static_cast<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 static_cast<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 248 : void SAL_CALL RootItemContainer::getFastPropertyValue( com::sun::star::uno::Any& aValue ,
295 : sal_Int32 nHandle ) const
296 : {
297 248 : switch( nHandle )
298 : {
299 : case PROPHANDLE_UINAME:
300 248 : aValue <<= m_aUIName;
301 248 : break;
302 : }
303 248 : }
304 :
305 496 : ::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 496 : if( pInfoHelper == NULL )
313 : {
314 : // Ready for multithreading
315 43 : osl::MutexGuard aGuard( osl::Mutex::getGlobalMutex() );
316 :
317 : // Control this pointer again, another instance can be faster then these!
318 43 : 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 43 : static ::cppu::OPropertyArrayHelper aInfoHelper( impl_getStaticPropertyDescriptor(), sal_True );
324 43 : pInfoHelper = &aInfoHelper;
325 43 : }
326 : }
327 :
328 496 : 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 43 : 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 43 : cppu::UnoType<OUString>::get(),
369 : com::sun::star::beans::PropertyAttribute::TRANSIENT )
370 129 : };
371 : // Use it to initialize sequence!
372 43 : const com::sun::star::uno::Sequence< com::sun::star::beans::Property > lPropertyDescriptor( pProperties, PROPCOUNT );
373 : // Return "PropertyDescriptor"
374 86 : return lPropertyDescriptor;
375 : }
376 :
377 : } // namespace framework
378 :
379 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|