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