1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
32*cdf0e10cSrcweir #include <cppuhelper/weakagg.hxx>
33*cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx>
34*cdf0e10cSrcweir #include <comphelper/propertysethelper.hxx>
35*cdf0e10cSrcweir #include <osl/mutex.hxx>
36*cdf0e10cSrcweir #include <comphelper/genericpropertyset.hxx>
37*cdf0e10cSrcweir #include <comphelper/propertysetinfo.hxx>
38*cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
39*cdf0e10cSrcweir #include <vos/mutex.hxx>
40*cdf0e10cSrcweir #include <rtl/uuid.h>
41*cdf0e10cSrcweir #include <boost/mem_fn.hpp>
42*cdf0e10cSrcweir #include <boost/bind.hpp>
43*cdf0e10cSrcweir #include <boost/utility.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using namespace ::rtl;
48*cdf0e10cSrcweir using namespace ::osl;
49*cdf0e10cSrcweir using namespace ::cppu;
50*cdf0e10cSrcweir using namespace ::comphelper;
51*cdf0e10cSrcweir using namespace ::com::sun::star;
52*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
53*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
54*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir DECLARE_STL_USTRINGACCESS_MAP( Any, GenericAnyMapImpl );
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace comphelper
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	struct IMPL_GenericPropertySet_MutexContainer
61*cdf0e10cSrcweir 	{
62*cdf0e10cSrcweir 		Mutex maMutex ;
63*cdf0e10cSrcweir 	} ;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	class GenericPropertySet :	public OWeakAggObject,
66*cdf0e10cSrcweir 								public XServiceInfo,
67*cdf0e10cSrcweir 								public XTypeProvider,
68*cdf0e10cSrcweir 								public PropertySetHelper,
69*cdf0e10cSrcweir 								private IMPL_GenericPropertySet_MutexContainer
70*cdf0e10cSrcweir 	{
71*cdf0e10cSrcweir 	private:
72*cdf0e10cSrcweir 		GenericAnyMapImpl	maAnyMap;
73*cdf0e10cSrcweir         ::cppu::OMultiTypeInterfaceContainerHelperVar< ::rtl::OUString,UStringHash,UStringEqual> m_aListener;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	protected:
76*cdf0e10cSrcweir 		virtual void _setPropertyValues( const PropertyMapEntry** ppEntries, const  Any* pValues ) throw( UnknownPropertyException,  PropertyVetoException,  IllegalArgumentException,  WrappedTargetException );
77*cdf0e10cSrcweir 		virtual void _getPropertyValues( const PropertyMapEntry** ppEntries,  Any* pValue ) throw( UnknownPropertyException,  WrappedTargetException );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	public:
80*cdf0e10cSrcweir 		GenericPropertySet( PropertySetInfo* pInfo ) throw();
81*cdf0e10cSrcweir 		virtual ~GenericPropertySet() throw();
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 		// XInterface
84*cdf0e10cSrcweir 		virtual  Any SAL_CALL queryAggregation( const  Type & rType ) throw( RuntimeException);
85*cdf0e10cSrcweir 		virtual  Any SAL_CALL queryInterface( const  Type & rType ) throw( RuntimeException);
86*cdf0e10cSrcweir 		virtual void SAL_CALL acquire() throw();
87*cdf0e10cSrcweir 		virtual void SAL_CALL release() throw();
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 		// XTypeProvider
90*cdf0e10cSrcweir 		virtual  Sequence<  Type > SAL_CALL getTypes(  ) throw( RuntimeException);
91*cdf0e10cSrcweir 		virtual  Sequence< sal_Int8 > SAL_CALL getImplementationId(  ) throw( RuntimeException);
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 		// XServiceInfo
94*cdf0e10cSrcweir 		virtual rtl::OUString SAL_CALL getImplementationName() throw(  RuntimeException );
95*cdf0e10cSrcweir 		virtual sal_Bool SAL_CALL supportsService( const rtl::OUString& ServiceName ) throw(  RuntimeException );
96*cdf0e10cSrcweir 		virtual  Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() throw(  RuntimeException );
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         // XPropertySet
99*cdf0e10cSrcweir         virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener ) throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
100*cdf0e10cSrcweir         virtual void SAL_CALL removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& aListener ) throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException);
101*cdf0e10cSrcweir 	};
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir GenericPropertySet::GenericPropertySet( PropertySetInfo* pInfo ) throw()
108*cdf0e10cSrcweir : PropertySetHelper( pInfo )
109*cdf0e10cSrcweir ,m_aListener(maMutex)
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir GenericPropertySet::~GenericPropertySet() throw()
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::addPropertyChangeListener( const ::rtl::OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir     Reference < XPropertySetInfo > xInfo = getPropertySetInfo(  );
119*cdf0e10cSrcweir     if ( xInfo.is() )
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         if ( !aPropertyName.getLength() )
122*cdf0e10cSrcweir         {
123*cdf0e10cSrcweir             Sequence< Property> aSeq = xInfo->getProperties();
124*cdf0e10cSrcweir             const Property* pIter = aSeq.getConstArray();
125*cdf0e10cSrcweir             const Property* pEnd  = pIter + aSeq.getLength();
126*cdf0e10cSrcweir             for( ; pIter != pEnd ; ++pIter)
127*cdf0e10cSrcweir             {
128*cdf0e10cSrcweir                 m_aListener.addInterface(pIter->Name,xListener);
129*cdf0e10cSrcweir             }
130*cdf0e10cSrcweir         }
131*cdf0e10cSrcweir         else if ( xInfo->hasPropertyByName(aPropertyName) )
132*cdf0e10cSrcweir 	        m_aListener.addInterface(aPropertyName,xListener);
133*cdf0e10cSrcweir         else
134*cdf0e10cSrcweir             throw UnknownPropertyException( aPropertyName, *this );
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::removePropertyChangeListener( const ::rtl::OUString& aPropertyName, const Reference< XPropertyChangeListener >& xListener ) throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     ResettableMutexGuard aGuard( maMutex );
141*cdf0e10cSrcweir     Reference < XPropertySetInfo > xInfo = getPropertySetInfo(  );
142*cdf0e10cSrcweir     aGuard.clear();
143*cdf0e10cSrcweir     if ( xInfo.is() )
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir         if ( !aPropertyName.getLength() )
146*cdf0e10cSrcweir         {
147*cdf0e10cSrcweir             Sequence< Property> aSeq = xInfo->getProperties();
148*cdf0e10cSrcweir             const Property* pIter = aSeq.getConstArray();
149*cdf0e10cSrcweir             const Property* pEnd  = pIter + aSeq.getLength();
150*cdf0e10cSrcweir             for( ; pIter != pEnd ; ++pIter)
151*cdf0e10cSrcweir             {
152*cdf0e10cSrcweir                 m_aListener.removeInterface(pIter->Name,xListener);
153*cdf0e10cSrcweir             }
154*cdf0e10cSrcweir         }
155*cdf0e10cSrcweir         else if ( xInfo->hasPropertyByName(aPropertyName) )
156*cdf0e10cSrcweir 	        m_aListener.removeInterface(aPropertyName,xListener);
157*cdf0e10cSrcweir         else
158*cdf0e10cSrcweir             throw UnknownPropertyException( aPropertyName, *this );
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir void GenericPropertySet::_setPropertyValues( const PropertyMapEntry** ppEntries, const Any* pValues )
163*cdf0e10cSrcweir 	throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException )
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir 	ResettableMutexGuard aGuard( maMutex );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	while( *ppEntries )
168*cdf0e10cSrcweir 	{
169*cdf0e10cSrcweir 		const OUString aPropertyName( (*ppEntries)->mpName, (*ppEntries)->mnNameLen, RTL_TEXTENCODING_ASCII_US );
170*cdf0e10cSrcweir         OInterfaceContainerHelper * pHelper = m_aListener.getContainer(aPropertyName);
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 		maAnyMap[ aPropertyName ] = *pValues;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         if ( pHelper )
175*cdf0e10cSrcweir         {
176*cdf0e10cSrcweir             PropertyChangeEvent aEvt;
177*cdf0e10cSrcweir             aEvt.PropertyName = aPropertyName;
178*cdf0e10cSrcweir             aEvt.NewValue = *pValues;
179*cdf0e10cSrcweir             aGuard.clear();
180*cdf0e10cSrcweir             pHelper->notifyEach( &XPropertyChangeListener::propertyChange, aEvt );
181*cdf0e10cSrcweir             aGuard.reset();
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 		ppEntries++;
185*cdf0e10cSrcweir 		pValues++;
186*cdf0e10cSrcweir 	}
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir void GenericPropertySet::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, Any* pValue )
190*cdf0e10cSrcweir 	throw( UnknownPropertyException, WrappedTargetException )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir 	MutexGuard aGuard( maMutex );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	while( *ppEntries )
195*cdf0e10cSrcweir 	{
196*cdf0e10cSrcweir 		const OUString aPropertyName( (*ppEntries)->mpName, (*ppEntries)->mnNameLen, RTL_TEXTENCODING_ASCII_US );
197*cdf0e10cSrcweir 		*pValue = maAnyMap[ aPropertyName ];
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		ppEntries++;
200*cdf0e10cSrcweir 		pValue++;
201*cdf0e10cSrcweir 	}
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir // XInterface
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir Any SAL_CALL GenericPropertySet::queryInterface( const Type & rType )
207*cdf0e10cSrcweir 	throw( RuntimeException )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	return OWeakAggObject::queryInterface( rType );
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir Any SAL_CALL GenericPropertySet::queryAggregation( const Type & rType )
213*cdf0e10cSrcweir 	throw(RuntimeException)
214*cdf0e10cSrcweir {
215*cdf0e10cSrcweir 	Any aAny;
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	if( rType == ::getCppuType((const Reference< XServiceInfo >*)0) )
218*cdf0e10cSrcweir 		aAny <<= Reference< XServiceInfo >(this);
219*cdf0e10cSrcweir 	else if( rType == ::getCppuType((const Reference< XTypeProvider >*)0) )
220*cdf0e10cSrcweir 		aAny <<= Reference< XTypeProvider >(this);
221*cdf0e10cSrcweir 	else if( rType == ::getCppuType((const Reference< XPropertySet >*)0) )
222*cdf0e10cSrcweir 		aAny <<= Reference< XPropertySet >(this);
223*cdf0e10cSrcweir 	else if( rType == ::getCppuType((const Reference< XMultiPropertySet >*)0) )
224*cdf0e10cSrcweir 		aAny <<= Reference< XMultiPropertySet >(this);
225*cdf0e10cSrcweir 	else
226*cdf0e10cSrcweir 		aAny <<= OWeakAggObject::queryAggregation( rType );
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 	return aAny;
229*cdf0e10cSrcweir }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::acquire() throw()
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	OWeakAggObject::acquire();
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir void SAL_CALL GenericPropertySet::release() throw()
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir 	OWeakAggObject::release();
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir uno::Sequence< uno::Type > SAL_CALL GenericPropertySet::getTypes()
242*cdf0e10cSrcweir 	throw (uno::RuntimeException)
243*cdf0e10cSrcweir {
244*cdf0e10cSrcweir 	uno::Sequence< uno::Type > aTypes( 5 );
245*cdf0e10cSrcweir 	uno::Type* pTypes = aTypes.getArray();
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 	*pTypes++ = ::getCppuType((const uno::Reference< XAggregation>*)0);
248*cdf0e10cSrcweir 	*pTypes++ = ::getCppuType((const uno::Reference< XServiceInfo>*)0);
249*cdf0e10cSrcweir 	*pTypes++ = ::getCppuType((const uno::Reference< XTypeProvider>*)0);
250*cdf0e10cSrcweir 	*pTypes++ = ::getCppuType((const uno::Reference< XPropertySet>*)0);
251*cdf0e10cSrcweir 	*pTypes++ = ::getCppuType((const uno::Reference< XMultiPropertySet>*)0);
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir 	return aTypes;
254*cdf0e10cSrcweir }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir uno::Sequence< sal_Int8 > SAL_CALL GenericPropertySet::getImplementationId()
257*cdf0e10cSrcweir 	throw (uno::RuntimeException)
258*cdf0e10cSrcweir {
259*cdf0e10cSrcweir 	MutexGuard aGuard( maMutex );
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 	static uno::Sequence< sal_Int8 > aId;
262*cdf0e10cSrcweir 	if( aId.getLength() == 0 )
263*cdf0e10cSrcweir 	{
264*cdf0e10cSrcweir 		aId.realloc( 16 );
265*cdf0e10cSrcweir 		rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
266*cdf0e10cSrcweir 	}
267*cdf0e10cSrcweir 	return aId;
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir // XServiceInfo
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir sal_Bool SAL_CALL GenericPropertySet::supportsService( const  OUString& ServiceName ) throw(RuntimeException)
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir     Sequence< OUString > aSNL( getSupportedServiceNames() );
275*cdf0e10cSrcweir     const OUString * pArray = aSNL.getConstArray();
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     for( sal_Int32 i = 0; i < aSNL.getLength(); ++i )
278*cdf0e10cSrcweir         if( pArray[i] == ServiceName )
279*cdf0e10cSrcweir             return sal_True;
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     return sal_False;
282*cdf0e10cSrcweir }
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir OUString SAL_CALL GenericPropertySet::getImplementationName() throw( RuntimeException )
285*cdf0e10cSrcweir {
286*cdf0e10cSrcweir 	return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.comphelper.GenericPropertySet") );
287*cdf0e10cSrcweir }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir Sequence< OUString > SAL_CALL GenericPropertySet::getSupportedServiceNames(  )
290*cdf0e10cSrcweir 	throw( RuntimeException )
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir     Sequence< OUString > aSNS( 1 );
293*cdf0e10cSrcweir     aSNS.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.beans.XPropertySet" ));
294*cdf0e10cSrcweir     return aSNS;
295*cdf0e10cSrcweir }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > comphelper::GenericPropertySet_CreateInstance( comphelper::PropertySetInfo* pInfo )
298*cdf0e10cSrcweir {
299*cdf0e10cSrcweir 	return (XPropertySet*)new GenericPropertySet( pInfo );
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302