CWS-TOOLING: integrate CWS os146
[LibreOffice.git] / comphelper / source / property / propstate.cxx
blob214bccb3f40f35e3943203d7a78f87bbecf9fc35
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/propstate.hxx>
31 #include <com/sun/star/uno/genfunc.h>
32 #include <cppuhelper/queryinterface.hxx>
33 #include <comphelper/sequence.hxx>
35 //.........................................................................
36 namespace comphelper
38 //.........................................................................
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::Type;
42 using ::com::sun::star::uno::RuntimeException;
43 using ::com::sun::star::uno::Sequence;
44 using ::com::sun::star::lang::XTypeProvider;
45 using ::com::sun::star::uno::Any;
46 using ::com::sun::star::uno::cpp_queryInterface;
47 using ::com::sun::star::uno::cpp_release;
48 using ::com::sun::star::beans::PropertyState_DEFAULT_VALUE;
49 using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
51 //=====================================================================
52 // OPropertyStateHelper
53 //=====================================================================
55 //---------------------------------------------------------------------
56 ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::queryInterface(const ::com::sun::star::uno::Type& _rType) throw( ::com::sun::star::uno::RuntimeException)
58 ::com::sun::star::uno::Any aReturn = OPropertySetHelper::queryInterface(_rType);
59 // our own ifaces
60 if ( !aReturn.hasValue() )
61 aReturn = ::cppu::queryInterface(_rType, static_cast< ::com::sun::star::beans::XPropertyState*>(this));
63 return aReturn;
66 //---------------------------------------------------------------------
67 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> OPropertyStateHelper::getTypes() throw( ::com::sun::star::uno::RuntimeException)
69 static ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> aTypes;
70 if (!aTypes.getLength())
72 aTypes.realloc(4);
73 ::com::sun::star::uno::Type* pTypes = aTypes.getArray();
74 // base class types
75 pTypes[0] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>*)NULL);
76 pTypes[1] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>*)NULL);
77 pTypes[2] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>*)NULL);
78 // my own type
79 pTypes[3] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState>*)NULL);
81 return aTypes;
84 OPropertyStateHelper::OPropertyStateHelper(
85 ::cppu::OBroadcastHelper& rBHlp,
86 ::cppu::IEventNotificationHook *i_pFireEvents)
87 : ::cppu::OPropertySetHelper(rBHlp, i_pFireEvents) { }
89 OPropertyStateHelper::~OPropertyStateHelper() {}
91 //---------------------------------------------------------------------
92 void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const ::com::sun::star::uno::Any& aNewValue, const ::com::sun::star::uno::Any& aOldValue)
94 fire(&nHandle, &aNewValue, &aOldValue, 1, sal_False);
97 // XPropertyState
98 //---------------------------------------------------------------------
99 ::com::sun::star::beans::PropertyState SAL_CALL OPropertyStateHelper::getPropertyState(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException)
101 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
102 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
104 if (nHandle == -1)
105 throw ::com::sun::star::beans::UnknownPropertyException();
107 return getPropertyStateByHandle(nHandle);
110 //---------------------------------------------------------------------
111 void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException)
113 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
114 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
116 if (nHandle == -1)
117 throw ::com::sun::star::beans::UnknownPropertyException();
119 setPropertyToDefaultByHandle(nHandle);
122 //---------------------------------------------------------------------
123 ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::getPropertyDefault(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException)
125 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
126 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
128 if (nHandle == -1)
129 throw ::com::sun::star::beans::UnknownPropertyException();
131 return getPropertyDefaultByHandle(nHandle);
134 //---------------------------------------------------------------------
135 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> SAL_CALL OPropertyStateHelper::getPropertyStates(const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rPropertyNames) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException)
137 sal_Int32 nLen = _rPropertyNames.getLength();
138 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> aRet(nLen);
139 ::com::sun::star::beans::PropertyState* pValues = aRet.getArray();
140 const ::rtl::OUString* pNames = _rPropertyNames.getConstArray();
142 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
144 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property> aProps = rHelper.getProperties();
145 const ::com::sun::star::beans::Property* pProps = aProps.getConstArray();
146 sal_Int32 nPropCount = aProps.getLength();
148 osl::MutexGuard aGuard(rBHelper.rMutex);
149 for (sal_Int32 i=0, j=0; i<nPropCount && j<nLen; ++i, ++pProps)
151 // get the values only for valid properties
152 if (pProps->Name.equals(*pNames))
154 *pValues = getPropertyState(*pNames);
155 ++pValues;
156 ++pNames;
157 ++j;
161 return aRet;
164 //---------------------------------------------------------------------
165 ::com::sun::star::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
167 // simply compare the current and the default value
168 Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
169 Any aDefaultValue; getFastPropertyValue( aDefaultValue, _nHandle );
171 sal_Bool bEqual = uno_type_equalData(
172 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
173 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
174 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
175 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
177 return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
180 //---------------------------------------------------------------------
181 void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
183 setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
186 //---------------------------------------------------------------------
187 ::com::sun::star::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
189 return ::com::sun::star::uno::Any();
192 //=====================================================================
193 // OStatefulPropertySet
194 //=====================================================================
195 //---------------------------------------------------------------------
196 OStatefulPropertySet::OStatefulPropertySet()
197 :OPropertyStateHelper( GetBroadcastHelper() )
201 //---------------------------------------------------------------------
202 OStatefulPropertySet::~OStatefulPropertySet()
206 //---------------------------------------------------------------------
207 Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes() throw(RuntimeException)
209 Sequence< Type > aOwnTypes( 2 );
210 aOwnTypes[0] = XWeak::static_type();
211 aOwnTypes[1] = XTypeProvider::static_type();
213 return concatSequences(
214 aOwnTypes,
215 OPropertyStateHelper::getTypes()
219 //---------------------------------------------------------------------
220 Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId() throw(RuntimeException)
222 static ::cppu::OImplementationId * pId = NULL;
223 if ( !pId )
225 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
226 if ( !pId )
228 static ::cppu::OImplementationId aId;
229 pId = &aId;
232 return pId->getImplementationId();
235 //---------------------------------------------------------------------
236 Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType ) throw(RuntimeException)
238 Any aReturn = OWeakObject::queryInterface( _rType );
239 if ( !aReturn.hasValue() )
240 aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
241 if ( !aReturn.hasValue() )
242 aReturn = OPropertyStateHelper::queryInterface( _rType );
243 return aReturn;
246 //---------------------------------------------------------------------
247 void SAL_CALL OStatefulPropertySet::acquire() throw()
249 ::cppu::OWeakObject::acquire();
252 //---------------------------------------------------------------------
253 void SAL_CALL OStatefulPropertySet::release() throw()
255 ::cppu::OWeakObject::release();
258 //.........................................................................
260 //.........................................................................