1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d739b60SAndrew Rist  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19*6d739b60SAndrew Rist  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir //_________________________________________________________________________________________________________________
27cdf0e10cSrcweir //	my own includes
28cdf0e10cSrcweir //_________________________________________________________________________________________________________________
29cdf0e10cSrcweir #include <uielement/itemcontainer.hxx>
30cdf0e10cSrcweir #include <uielement/constitemcontainer.hxx>
31cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //_________________________________________________________________________________________________________________
34cdf0e10cSrcweir //	other includes
35cdf0e10cSrcweir //_________________________________________________________________________________________________________________
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace cppu;
38cdf0e10cSrcweir using namespace com::sun::star::uno;
39cdf0e10cSrcweir using namespace com::sun::star::lang;
40cdf0e10cSrcweir using namespace com::sun::star::beans;
41cdf0e10cSrcweir using namespace com::sun::star::container;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir const char WRONG_TYPE_EXCEPTION[] = "Type must be com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >";
44cdf0e10cSrcweir 
45cdf0e10cSrcweir namespace framework
46cdf0e10cSrcweir {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //*****************************************************************************************************************
49cdf0e10cSrcweir //	XInterface, XTypeProvider
50cdf0e10cSrcweir //*****************************************************************************************************************
51cdf0e10cSrcweir 
ItemContainer(const ShareableMutex & rMutex)52cdf0e10cSrcweir ItemContainer::ItemContainer( const ShareableMutex& rMutex ) :
53cdf0e10cSrcweir     m_aShareMutex( rMutex )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 
ItemContainer(const ConstItemContainer & rConstItemContainer,const ShareableMutex & rMutex)58cdf0e10cSrcweir ItemContainer::ItemContainer( const ConstItemContainer& rConstItemContainer, const ShareableMutex& rMutex ) : m_aShareMutex( rMutex )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     copyItemContainer( rConstItemContainer.m_aItemVector, rMutex );
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
ItemContainer(const Reference<XIndexAccess> & rSourceContainer,const ShareableMutex & rMutex)63cdf0e10cSrcweir ItemContainer::ItemContainer( const Reference< XIndexAccess >& rSourceContainer, const ShareableMutex& rMutex ) :
64cdf0e10cSrcweir     m_aShareMutex( rMutex )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir     if ( rSourceContainer.is() )
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         sal_Int32 nCount = rSourceContainer->getCount();
69cdf0e10cSrcweir         try
70cdf0e10cSrcweir         {
71cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < nCount; i++ )
72cdf0e10cSrcweir             {
73cdf0e10cSrcweir                 Sequence< PropertyValue > aPropSeq;
74cdf0e10cSrcweir                 if ( rSourceContainer->getByIndex( i ) >>= aPropSeq )
75cdf0e10cSrcweir                 {
76cdf0e10cSrcweir                     sal_Int32 nContainerIndex = -1;
77cdf0e10cSrcweir                     Reference< XIndexAccess > xIndexAccess;
78cdf0e10cSrcweir                     for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
79cdf0e10cSrcweir                     {
80cdf0e10cSrcweir                         if ( aPropSeq[j].Name.equalsAscii( "ItemDescriptorContainer" ))
81cdf0e10cSrcweir                         {
82cdf0e10cSrcweir                             aPropSeq[j].Value >>= xIndexAccess;
83cdf0e10cSrcweir                             nContainerIndex = j;
84cdf0e10cSrcweir                             break;
85cdf0e10cSrcweir                         }
86cdf0e10cSrcweir                     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir                     if ( xIndexAccess.is() && nContainerIndex >= 0 )
89cdf0e10cSrcweir                         aPropSeq[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess, rMutex );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir                     m_aItemVector.push_back( aPropSeq );
92cdf0e10cSrcweir                 }
93cdf0e10cSrcweir             }
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir         catch ( IndexOutOfBoundsException& )
96cdf0e10cSrcweir         {
97cdf0e10cSrcweir         }
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
~ItemContainer()101cdf0e10cSrcweir ItemContainer::~ItemContainer()
102cdf0e10cSrcweir {
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir // private
copyItemContainer(const std::vector<Sequence<PropertyValue>> & rSourceVector,const ShareableMutex & rMutex)106cdf0e10cSrcweir void ItemContainer::copyItemContainer( const std::vector< Sequence< PropertyValue > >& rSourceVector, const ShareableMutex& rMutex )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     const sal_uInt32 nCount = rSourceVector.size();
109cdf0e10cSrcweir     for ( sal_uInt32 i = 0; i < nCount; ++i )
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         sal_Int32 nContainerIndex = -1;
112cdf0e10cSrcweir         Sequence< PropertyValue > aPropSeq( rSourceVector[i] );
113cdf0e10cSrcweir         Reference< XIndexAccess > xIndexAccess;
114cdf0e10cSrcweir         for ( sal_Int32 j = 0; j < aPropSeq.getLength(); j++ )
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             if ( aPropSeq[j].Name.equalsAscii( "ItemDescriptorContainer" ))
117cdf0e10cSrcweir             {
118cdf0e10cSrcweir                 aPropSeq[j].Value >>= xIndexAccess;
119cdf0e10cSrcweir                 nContainerIndex = j;
120cdf0e10cSrcweir                 break;
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         if ( xIndexAccess.is() && nContainerIndex >= 0 )
125cdf0e10cSrcweir             aPropSeq[nContainerIndex].Value <<= deepCopyContainer( xIndexAccess, rMutex );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         m_aItemVector.push_back( aPropSeq );
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
deepCopyContainer(const Reference<XIndexAccess> & rSubContainer,const ShareableMutex & rMutex)131cdf0e10cSrcweir Reference< XIndexAccess > ItemContainer::deepCopyContainer( const Reference< XIndexAccess >& rSubContainer, const ShareableMutex& rMutex )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     Reference< XIndexAccess > xReturn;
134cdf0e10cSrcweir     if ( rSubContainer.is() )
135cdf0e10cSrcweir     {
136cdf0e10cSrcweir         ConstItemContainer* pSource = ConstItemContainer::GetImplementation( rSubContainer );
137cdf0e10cSrcweir         ItemContainer* pSubContainer( 0 );
138cdf0e10cSrcweir         if ( pSource )
139cdf0e10cSrcweir             pSubContainer = new ItemContainer( *pSource, rMutex );
140cdf0e10cSrcweir         else
141cdf0e10cSrcweir             pSubContainer = new ItemContainer( rSubContainer, rMutex );
142cdf0e10cSrcweir         xReturn = Reference< XIndexAccess >( static_cast< OWeakObject* >( pSubContainer ), UNO_QUERY );
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     return xReturn;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir // XUnoTunnel
getSomething(const::com::sun::star::uno::Sequence<sal_Int8> & rIdentifier)149cdf0e10cSrcweir sal_Int64 ItemContainer::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rIdentifier ) throw(::com::sun::star::uno::RuntimeException)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     if( ( rIdentifier.getLength() == 16 ) && ( 0 == rtl_compareMemory( ItemContainer::GetUnoTunnelId().getConstArray(), rIdentifier.getConstArray(), 16 ) ) )
152cdf0e10cSrcweir         return sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( this ));
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     return 0;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
GetUnoTunnelId()157cdf0e10cSrcweir const Sequence< sal_Int8 >& ItemContainer::GetUnoTunnelId() throw()
158cdf0e10cSrcweir {
159cdf0e10cSrcweir 	static ::com::sun::star::uno::Sequence< sal_Int8 > * pSeq = NULL;
160cdf0e10cSrcweir 	if( !pSeq )
161cdf0e10cSrcweir 	{
162cdf0e10cSrcweir 		::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() );
163cdf0e10cSrcweir 		if( !pSeq )
164cdf0e10cSrcweir 		{
165cdf0e10cSrcweir 			static ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( 16 );
166cdf0e10cSrcweir 			rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
167cdf0e10cSrcweir 			pSeq = &aSeq;
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 	}
170cdf0e10cSrcweir 	return *pSeq;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
GetImplementation(const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & rxIFace)173cdf0e10cSrcweir ItemContainer* ItemContainer::GetImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rxIFace ) throw()
174cdf0e10cSrcweir {
175cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xUT( rxIFace, ::com::sun::star::uno::UNO_QUERY );
176cdf0e10cSrcweir 	return xUT.is() ? reinterpret_cast< ItemContainer* >(sal::static_int_cast< sal_IntPtr >(
177cdf0e10cSrcweir                           xUT->getSomething( ItemContainer::GetUnoTunnelId() ))) : NULL;
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir // XElementAccess
hasElements()181cdf0e10cSrcweir sal_Bool SAL_CALL ItemContainer::hasElements()
182cdf0e10cSrcweir throw ( RuntimeException )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     ShareGuard aLock( m_aShareMutex );
185cdf0e10cSrcweir     return ( !m_aItemVector.empty() );
186cdf0e10cSrcweir }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir // XIndexAccess
getCount()189cdf0e10cSrcweir sal_Int32 SAL_CALL ItemContainer::getCount()
190cdf0e10cSrcweir throw ( RuntimeException )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir     ShareGuard aLock( m_aShareMutex );
193cdf0e10cSrcweir     return m_aItemVector.size();
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
getByIndex(sal_Int32 Index)196cdf0e10cSrcweir Any SAL_CALL ItemContainer::getByIndex( sal_Int32 Index )
197cdf0e10cSrcweir throw ( IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir     ShareGuard aLock( m_aShareMutex );
200cdf0e10cSrcweir     if ( sal_Int32( m_aItemVector.size()) > Index )
201cdf0e10cSrcweir         return makeAny( m_aItemVector[Index] );
202cdf0e10cSrcweir     else
203cdf0e10cSrcweir 		throw IndexOutOfBoundsException( ::rtl::OUString(), (OWeakObject *)this );
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir // XIndexContainer
insertByIndex(sal_Int32 Index,const Any & aItem)207cdf0e10cSrcweir void SAL_CALL ItemContainer::insertByIndex( sal_Int32 Index, const Any& aItem )
208cdf0e10cSrcweir throw ( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir     Sequence< PropertyValue > aSeq;
211cdf0e10cSrcweir     if ( aItem >>= aSeq )
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         ShareGuard aLock( m_aShareMutex );
214cdf0e10cSrcweir         if ( sal_Int32( m_aItemVector.size()) == Index )
215cdf0e10cSrcweir             m_aItemVector.push_back( aSeq );
216cdf0e10cSrcweir         else if ( sal_Int32( m_aItemVector.size()) >Index )
217cdf0e10cSrcweir 	    {
218cdf0e10cSrcweir 		    std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin();
219cdf0e10cSrcweir             aIter += Index;
220cdf0e10cSrcweir 		    m_aItemVector.insert( aIter, aSeq );
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir         else
223cdf0e10cSrcweir 		    throw IndexOutOfBoundsException( ::rtl::OUString(), (OWeakObject *)this );
224cdf0e10cSrcweir     }
225cdf0e10cSrcweir     else
226cdf0e10cSrcweir         throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( WRONG_TYPE_EXCEPTION )),
227cdf0e10cSrcweir 				                        (OWeakObject *)this, 2 );
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
removeByIndex(sal_Int32 Index)230cdf0e10cSrcweir void SAL_CALL ItemContainer::removeByIndex( sal_Int32 Index )
231cdf0e10cSrcweir throw ( IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     ShareGuard aLock( m_aShareMutex );
234cdf0e10cSrcweir 	if ( (sal_Int32)m_aItemVector.size() > Index )
235cdf0e10cSrcweir 	{
236cdf0e10cSrcweir 		std::vector< Sequence< PropertyValue > >::iterator aIter = m_aItemVector.begin();
237cdf0e10cSrcweir 		aIter += Index;
238cdf0e10cSrcweir 		m_aItemVector.erase( aIter );
239cdf0e10cSrcweir 	}
240cdf0e10cSrcweir 	else
241cdf0e10cSrcweir 		throw IndexOutOfBoundsException( ::rtl::OUString(), (OWeakObject *)this );
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
replaceByIndex(sal_Int32 Index,const Any & aItem)244cdf0e10cSrcweir void SAL_CALL ItemContainer::replaceByIndex( sal_Int32 Index, const Any& aItem )
245cdf0e10cSrcweir throw ( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException )
246cdf0e10cSrcweir {
247cdf0e10cSrcweir     Sequence< PropertyValue > aSeq;
248cdf0e10cSrcweir     if ( aItem >>= aSeq )
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir         ShareGuard aLock( m_aShareMutex );
251cdf0e10cSrcweir         if ( sal_Int32( m_aItemVector.size()) > Index )
252cdf0e10cSrcweir             m_aItemVector[Index] = aSeq;
253cdf0e10cSrcweir         else
254cdf0e10cSrcweir 		    throw IndexOutOfBoundsException( ::rtl::OUString(), (OWeakObject *)this );
255cdf0e10cSrcweir     }
256cdf0e10cSrcweir     else
257cdf0e10cSrcweir         throw IllegalArgumentException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( WRONG_TYPE_EXCEPTION )),
258cdf0e10cSrcweir 				                        (OWeakObject *)this, 2 );
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir } // namespace framework
262cdf0e10cSrcweir 
263