1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <uielement/constitemcontainer.hxx>
23 #include <uielement/rootitemcontainer.hxx>
24 #include <uielement/itemcontainer.hxx>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <cppuhelper/implbase1.hxx>
29 #include <comphelper/servicehelper.hxx>
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
;
37 const int PROPHANDLE_UINAME
= 1;
38 const int PROPCOUNT
= 1;
39 const char PROPNAME_UINAME
[] = "UIName";
45 * The class which implements the PropertySetInfo interface.
49 static int SAL_CALL
compare_OUString_Property_Impl( const void *arg1
, const void *arg2
)
51 return static_cast<OUString
const *>(arg1
)->compareTo( static_cast<Property
const *>(arg2
)->Name
);
55 class OPropertySetHelperInfo_Impl
56 : public WeakImplHelper1
< ::com::sun::star::beans::XPropertySetInfo
>
58 Sequence
< Property
> aInfos
;
61 OPropertySetHelperInfo_Impl( IPropertyArrayHelper
& rHelper_
);
63 // XPropertySetInfo-Methoden
64 virtual Sequence
< Property
> SAL_CALL
getProperties() throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
65 virtual Property SAL_CALL
getPropertyByName(const OUString
& PropertyName
) throw(::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
66 virtual sal_Bool SAL_CALL
hasPropertyByName(const OUString
& PropertyName
) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
70 * Create an object that implements XPropertySetInfo IPropertyArrayHelper.
72 OPropertySetHelperInfo_Impl::OPropertySetHelperInfo_Impl(
73 IPropertyArrayHelper
& rHelper_
)
74 :aInfos( rHelper_
.getProperties() )
79 * Return the sequence of properties, which are provided through the constructor.
81 Sequence
< Property
> OPropertySetHelperInfo_Impl::getProperties() throw(::com::sun::star::uno::RuntimeException
, std::exception
)
87 * Return the sequence of properties, which are provided through the constructor.
89 Property
OPropertySetHelperInfo_Impl::getPropertyByName( const OUString
& PropertyName
) throw(::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
92 pR
= static_cast<Property
*>(bsearch( &PropertyName
, aInfos
.getConstArray(), aInfos
.getLength(),
94 compare_OUString_Property_Impl
));
96 throw UnknownPropertyException();
103 * Return the sequence of properties, which are provided through the constructor.
105 sal_Bool
OPropertySetHelperInfo_Impl::hasPropertyByName( const OUString
& PropertyName
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
108 pR
= static_cast<Property
*>(bsearch( &PropertyName
, aInfos
.getConstArray(), aInfos
.getLength(),
110 compare_OUString_Property_Impl
));
114 ConstItemContainer::ConstItemContainer()
118 ConstItemContainer::ConstItemContainer( const ItemContainer
& rItemContainer
)
120 ShareGuard
aLock( rItemContainer
.m_aShareMutex
);
121 copyItemContainer( rItemContainer
.m_aItemVector
);
124 ConstItemContainer::ConstItemContainer( const Reference
< XIndexAccess
>& rSourceContainer
, bool bFastCopy
)
126 // We also have to copy the UIName property
129 Reference
< XPropertySet
> xPropSet( rSourceContainer
, UNO_QUERY
);
132 xPropSet
->getPropertyValue("UIName") >>= m_aUIName
;
135 catch ( const Exception
& )
139 if ( rSourceContainer
.is() )
143 sal_Int32 nCount
= rSourceContainer
->getCount();
144 m_aItemVector
.reserve(nCount
);
147 for ( sal_Int32 i
= 0; i
< nCount
; i
++ )
149 Sequence
< PropertyValue
> aPropSeq
;
150 if ( rSourceContainer
->getByIndex( i
) >>= aPropSeq
)
151 m_aItemVector
.push_back( aPropSeq
);
156 for ( sal_Int32 i
= 0; i
< nCount
; i
++ )
158 Sequence
< PropertyValue
> aPropSeq
;
159 if ( rSourceContainer
->getByIndex( i
) >>= aPropSeq
)
161 sal_Int32 nContainerIndex
= -1;
162 Reference
< XIndexAccess
> xIndexAccess
;
163 for ( sal_Int32 j
= 0; j
< aPropSeq
.getLength(); j
++ )
165 if ( aPropSeq
[j
].Name
== "ItemDescriptorContainer" )
167 aPropSeq
[j
].Value
>>= xIndexAccess
;
173 if ( xIndexAccess
.is() && nContainerIndex
>= 0 )
174 aPropSeq
[nContainerIndex
].Value
<<= deepCopyContainer( xIndexAccess
);
176 m_aItemVector
.push_back( aPropSeq
);
181 catch ( const IndexOutOfBoundsException
& )
187 ConstItemContainer::~ConstItemContainer()
192 void ConstItemContainer::copyItemContainer( const std::vector
< Sequence
< PropertyValue
> >& rSourceVector
)
194 const sal_uInt32 nCount
= rSourceVector
.size();
195 for ( sal_uInt32 i
= 0; i
< nCount
; i
++ )
197 sal_Int32 nContainerIndex
= -1;
198 Sequence
< PropertyValue
> aPropSeq( rSourceVector
[i
] );
199 Reference
< XIndexAccess
> xIndexAccess
;
200 for ( sal_Int32 j
= 0; j
< aPropSeq
.getLength(); j
++ )
202 if ( aPropSeq
[j
].Name
== "ItemDescriptorContainer" )
204 aPropSeq
[j
].Value
>>= xIndexAccess
;
210 if ( xIndexAccess
.is() && nContainerIndex
>= 0 )
211 aPropSeq
[nContainerIndex
].Value
<<= deepCopyContainer( xIndexAccess
);
213 m_aItemVector
.push_back( aPropSeq
);
217 Reference
< XIndexAccess
> ConstItemContainer::deepCopyContainer( const Reference
< XIndexAccess
>& rSubContainer
)
219 Reference
< XIndexAccess
> xReturn
;
220 if ( rSubContainer
.is() )
222 ItemContainer
* pSource
= ItemContainer::GetImplementation( rSubContainer
);
223 ConstItemContainer
* pSubContainer( 0 );
225 pSubContainer
= new ConstItemContainer( *pSource
);
227 pSubContainer
= new ConstItemContainer( rSubContainer
);
228 xReturn
= Reference
< XIndexAccess
>( static_cast< OWeakObject
* >( pSubContainer
), UNO_QUERY
);
235 sal_Int64
ConstItemContainer::getSomething( const ::com::sun::star::uno::Sequence
< sal_Int8
>& rIdentifier
) throw(::com::sun::star::uno::RuntimeException
, std::exception
)
237 if( ( rIdentifier
.getLength() == 16 ) && ( 0 == memcmp( ConstItemContainer::GetUnoTunnelId().getConstArray(), rIdentifier
.getConstArray(), 16 ) ) )
239 return reinterpret_cast< sal_Int64
>( this );
246 class theConstItemContainerUnoTunnelId
: public rtl::Static
< UnoTunnelIdInit
, theConstItemContainerUnoTunnelId
> {};
249 const Sequence
< sal_Int8
>& ConstItemContainer::GetUnoTunnelId() throw()
251 return theConstItemContainerUnoTunnelId::get().getSeq();
254 ConstItemContainer
* ConstItemContainer::GetImplementation( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& rxIFace
) throw()
256 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XUnoTunnel
> xUT( rxIFace
, ::com::sun::star::uno::UNO_QUERY
);
257 return xUT
.is() ? reinterpret_cast< ConstItemContainer
* >(sal::static_int_cast
< sal_IntPtr
>(
258 xUT
->getSomething( ConstItemContainer::GetUnoTunnelId() ))) : NULL
;
262 sal_Bool SAL_CALL
ConstItemContainer::hasElements()
263 throw ( RuntimeException
, std::exception
)
265 return ( !m_aItemVector
.empty() );
269 sal_Int32 SAL_CALL
ConstItemContainer::getCount()
270 throw ( RuntimeException
, std::exception
)
272 return m_aItemVector
.size();
275 Any SAL_CALL
ConstItemContainer::getByIndex( sal_Int32 Index
)
276 throw ( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
278 if ( sal_Int32( m_aItemVector
.size()) > Index
)
279 return makeAny( m_aItemVector
[Index
] );
281 throw IndexOutOfBoundsException( OUString(), (OWeakObject
*)this );
285 Reference
< XPropertySetInfo
> SAL_CALL
ConstItemContainer::getPropertySetInfo()
286 throw (::com::sun::star::uno::RuntimeException
, std::exception
)
288 // Optimize this method !
289 // We initialize a static variable only one time. And we don't must use a mutex at every call!
290 // For the first call; pInfo is NULL - for the second call pInfo is different from NULL!
291 static Reference
< XPropertySetInfo
>* pInfo
= NULL
;
295 // Ready for multithreading
296 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() );
297 // Control this pointer again, another instance can be faster then these!
300 // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
301 // (Use method "getInfoHelper()".)
302 static Reference
< XPropertySetInfo
> xInfo( createPropertySetInfo( getInfoHelper() ) );
310 void SAL_CALL
ConstItemContainer::setPropertyValue( const OUString
&, const Any
& )
311 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::beans::PropertyVetoException
, ::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
315 Any SAL_CALL
ConstItemContainer::getPropertyValue( const OUString
& PropertyName
)
316 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
318 if ( PropertyName
== PROPNAME_UINAME
)
319 return makeAny( m_aUIName
);
321 throw UnknownPropertyException();
324 void SAL_CALL
ConstItemContainer::addPropertyChangeListener( const OUString
&, const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertyChangeListener
>& )
325 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
329 void SAL_CALL
ConstItemContainer::removePropertyChangeListener( const OUString
&, const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertyChangeListener
>& )
330 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
332 // Only read-only properties - do nothing
335 void SAL_CALL
ConstItemContainer::addVetoableChangeListener( const OUString
&, const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XVetoableChangeListener
>& )
336 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
338 // Only read-only properties - do nothing
341 void SAL_CALL
ConstItemContainer::removeVetoableChangeListener( const OUString
&, const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XVetoableChangeListener
>& )
342 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
344 // Only read-only properties - do nothing
348 void SAL_CALL
ConstItemContainer::setFastPropertyValue( sal_Int32
, const ::com::sun::star::uno::Any
& )
349 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::beans::PropertyVetoException
, ::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
353 Any SAL_CALL
ConstItemContainer::getFastPropertyValue( sal_Int32 nHandle
)
354 throw (::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
356 if ( nHandle
== PROPHANDLE_UINAME
)
357 return makeAny( m_aUIName
);
359 throw UnknownPropertyException();
362 ::cppu::IPropertyArrayHelper
& SAL_CALL
ConstItemContainer::getInfoHelper()
364 // Optimize this method !
365 // We initialize a static variable only one time. And we don't must use a mutex at every call!
366 // For the first call; pInfoHelper is NULL - for the second call pInfoHelper is different from NULL!
367 static ::cppu::OPropertyArrayHelper
* pInfoHelper
= NULL
;
369 if( pInfoHelper
== NULL
)
371 // Ready for multithreading
372 osl::MutexGuard
aGuard( osl::Mutex::getGlobalMutex() );
374 // Control this pointer again, another instance can be faster then these!
375 if( pInfoHelper
== NULL
)
377 // Define static member to give structure of properties to baseclass "OPropertySetHelper".
378 // "impl_getStaticPropertyDescriptor" is a non exported and static function, who will define a static propertytable.
379 // "sal_True" say: Table is sorted by name.
380 static ::cppu::OPropertyArrayHelper
aInfoHelper( impl_getStaticPropertyDescriptor(), sal_True
);
381 pInfoHelper
= &aInfoHelper
;
385 return(*pInfoHelper
);
388 const com::sun::star::uno::Sequence
< com::sun::star::beans::Property
> ConstItemContainer::impl_getStaticPropertyDescriptor()
390 // Create a property array to initialize sequence!
391 // Table of all predefined properties of this class. Its used from OPropertySetHelper-class!
392 // Don't forget to change the defines (see begin of this file), if you add, change or delete a property in this list!!!
393 // It's necessary for methods of OPropertySetHelper.
395 // YOU MUST SORT FOLLOW TABLE BY NAME ALPHABETICAL !!!
397 const com::sun::star::beans::Property pProperties
[] =
399 com::sun::star::beans::Property( OUString(PROPNAME_UINAME
), PROPHANDLE_UINAME
,
400 cppu::UnoType
<OUString
>::get(),
401 com::sun::star::beans::PropertyAttribute::TRANSIENT
| com::sun::star::beans::PropertyAttribute::READONLY
)
403 // Use it to initialize sequence!
404 const com::sun::star::uno::Sequence
< com::sun::star::beans::Property
> lPropertyDescriptor( pProperties
, PROPCOUNT
);
405 // Return "PropertyDescriptor"
406 return lPropertyDescriptor
;
409 Reference
< XPropertySetInfo
> ConstItemContainer::createPropertySetInfo(
410 IPropertyArrayHelper
& rProperties
)
412 return static_cast< XPropertySetInfo
* >( new OPropertySetHelperInfo_Impl( rProperties
) );
415 } // namespace framework
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */