Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / property / propstate.cxx
blob90d8e370e73005343f767dc9c095005a71f1406c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <comphelper/propstate.hxx>
21 #include <com/sun/star/uno/genfunc.h>
22 #include <cppuhelper/queryinterface.hxx>
23 #include <comphelper/sequence.hxx>
25 namespace comphelper
29 using ::com::sun::star::uno::Reference;
30 using ::com::sun::star::uno::Type;
31 using ::com::sun::star::uno::RuntimeException;
32 using ::com::sun::star::uno::Sequence;
33 using ::com::sun::star::lang::XTypeProvider;
34 using ::com::sun::star::uno::Any;
35 using ::com::sun::star::uno::cpp_queryInterface;
36 using ::com::sun::star::uno::cpp_release;
37 using ::com::sun::star::beans::PropertyState_DEFAULT_VALUE;
38 using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
41 // OPropertyStateHelper
45 ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::queryInterface(const ::com::sun::star::uno::Type& _rType) throw( ::com::sun::star::uno::RuntimeException, std::exception)
47 ::com::sun::star::uno::Any aReturn = OPropertySetHelper2::queryInterface(_rType);
48 // our own ifaces
49 if ( !aReturn.hasValue() )
50 aReturn = ::cppu::queryInterface(_rType, static_cast< ::com::sun::star::beans::XPropertyState*>(this));
52 return aReturn;
56 css::uno::Sequence<css::uno::Type> OPropertyStateHelper::getTypes()
58 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> aTypes(4);
59 ::com::sun::star::uno::Type* pTypes = aTypes.getArray();
60 // base class types
61 pTypes[0] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>*)NULL);
62 pTypes[1] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>*)NULL);
63 pTypes[2] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>*)NULL);
64 // my own type
65 pTypes[3] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState>*)NULL);
66 return aTypes;
69 OPropertyStateHelper::OPropertyStateHelper(
70 ::cppu::OBroadcastHelper& rBHlp,
71 ::cppu::IEventNotificationHook *i_pFireEvents)
72 : ::cppu::OPropertySetHelper2(rBHlp, i_pFireEvents) { }
74 OPropertyStateHelper::~OPropertyStateHelper() {}
77 void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const ::com::sun::star::uno::Any& aNewValue, const ::com::sun::star::uno::Any& aOldValue)
79 fire(&nHandle, &aNewValue, &aOldValue, 1, sal_False);
82 // XPropertyState
84 ::com::sun::star::beans::PropertyState SAL_CALL OPropertyStateHelper::getPropertyState(const OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException, std::exception)
86 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
87 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
89 if (nHandle == -1)
90 throw ::com::sun::star::beans::UnknownPropertyException();
92 return getPropertyStateByHandle(nHandle);
96 void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException, std::exception)
98 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
99 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
101 if (nHandle == -1)
102 throw ::com::sun::star::beans::UnknownPropertyException();
104 setPropertyToDefaultByHandle(nHandle);
108 ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::getPropertyDefault(const OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception)
110 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
111 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
113 if (nHandle == -1)
114 throw ::com::sun::star::beans::UnknownPropertyException();
116 return getPropertyDefaultByHandle(nHandle);
120 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> SAL_CALL OPropertyStateHelper::getPropertyStates(const ::com::sun::star::uno::Sequence< OUString >& _rPropertyNames) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException, std::exception)
122 sal_Int32 nLen = _rPropertyNames.getLength();
123 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> aRet(nLen);
124 ::com::sun::star::beans::PropertyState* pValues = aRet.getArray();
125 const OUString* pNames = _rPropertyNames.getConstArray();
127 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
129 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property> aProps = rHelper.getProperties();
130 const ::com::sun::star::beans::Property* pProps = aProps.getConstArray();
131 sal_Int32 nPropCount = aProps.getLength();
133 osl::MutexGuard aGuard(rBHelper.rMutex);
134 for (sal_Int32 i=0, j=0; i<nPropCount && j<nLen; ++i, ++pProps)
136 // get the values only for valid properties
137 if (pProps->Name.equals(*pNames))
139 *pValues = getPropertyState(*pNames);
140 ++pValues;
141 ++pNames;
142 ++j;
146 return aRet;
150 ::com::sun::star::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
152 // simply compare the current and the default value
153 Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
154 Any aDefaultValue; getFastPropertyValue( aDefaultValue, _nHandle );
156 bool bEqual = uno_type_equalData(
157 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
158 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
159 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
160 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
162 return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
166 void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
168 setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
172 ::com::sun::star::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
174 return ::com::sun::star::uno::Any();
178 // OStatefulPropertySet
181 OStatefulPropertySet::OStatefulPropertySet()
182 :OPropertyStateHelper( GetBroadcastHelper() )
187 OStatefulPropertySet::~OStatefulPropertySet()
192 Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes() throw(RuntimeException, std::exception)
194 Sequence< Type > aOwnTypes( 2 );
195 aOwnTypes[0] = cppu::UnoType<XWeak>::get();
196 aOwnTypes[1] = cppu::UnoType<XTypeProvider>::get();
198 return concatSequences(
199 aOwnTypes,
200 OPropertyStateHelper::getTypes()
204 Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId() throw(RuntimeException, std::exception)
206 return css::uno::Sequence<sal_Int8>();
210 Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType ) throw(RuntimeException, std::exception)
212 Any aReturn = OWeakObject::queryInterface( _rType );
213 if ( !aReturn.hasValue() )
214 aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
215 if ( !aReturn.hasValue() )
216 aReturn = OPropertyStateHelper::queryInterface( _rType );
217 return aReturn;
221 void SAL_CALL OStatefulPropertySet::acquire() throw()
223 ::cppu::OWeakObject::acquire();
227 void SAL_CALL OStatefulPropertySet::release() throw()
229 ::cppu::OWeakObject::release();
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */