bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / property / propstate.cxx
blob8f6b90759cd63a4b99ab3b46373cedb4e0db3b69
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 return css::uno::Sequence<css::uno::Type>({
59 cppu::UnoType<css::beans::XPropertySet>::get(),
60 cppu::UnoType<css::beans::XMultiPropertySet>::get(),
61 cppu::UnoType<css::beans::XFastPropertySet>::get(),
62 cppu::UnoType<css::beans::XPropertySetOption>::get(),
63 cppu::UnoType<css::beans::XPropertyState>::get()});
66 OPropertyStateHelper::OPropertyStateHelper(
67 ::cppu::OBroadcastHelper& rBHlp,
68 ::cppu::IEventNotificationHook *i_pFireEvents)
69 : ::cppu::OPropertySetHelper2(rBHlp, i_pFireEvents) { }
71 OPropertyStateHelper::~OPropertyStateHelper() {}
74 void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const ::com::sun::star::uno::Any& aNewValue, const ::com::sun::star::uno::Any& aOldValue)
76 fire(&nHandle, &aNewValue, &aOldValue, 1, sal_False);
79 // XPropertyState
81 ::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)
83 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
84 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
86 if (nHandle == -1)
87 throw ::com::sun::star::beans::UnknownPropertyException();
89 return getPropertyStateByHandle(nHandle);
93 void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException, std::exception)
95 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
96 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
98 if (nHandle == -1)
99 throw ::com::sun::star::beans::UnknownPropertyException();
101 setPropertyToDefaultByHandle(nHandle);
105 ::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)
107 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
108 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
110 if (nHandle == -1)
111 throw ::com::sun::star::beans::UnknownPropertyException();
113 return getPropertyDefaultByHandle(nHandle);
117 ::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)
119 sal_Int32 nLen = _rPropertyNames.getLength();
120 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> aRet(nLen);
121 ::com::sun::star::beans::PropertyState* pValues = aRet.getArray();
122 const OUString* pNames = _rPropertyNames.getConstArray();
124 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
126 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property> aProps = rHelper.getProperties();
127 const ::com::sun::star::beans::Property* pProps = aProps.getConstArray();
128 sal_Int32 nPropCount = aProps.getLength();
130 osl::MutexGuard aGuard(rBHelper.rMutex);
131 for (sal_Int32 i=0, j=0; i<nPropCount && j<nLen; ++i, ++pProps)
133 // get the values only for valid properties
134 if (pProps->Name.equals(*pNames))
136 *pValues = getPropertyState(*pNames);
137 ++pValues;
138 ++pNames;
139 ++j;
143 return aRet;
147 ::com::sun::star::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
149 // simply compare the current and the default value
150 Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
151 Any aDefaultValue; getFastPropertyValue( aDefaultValue, _nHandle );
153 bool bEqual = uno_type_equalData(
154 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
155 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
156 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
157 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
159 return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
163 void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
165 setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
169 ::com::sun::star::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
171 return ::com::sun::star::uno::Any();
175 // OStatefulPropertySet
178 OStatefulPropertySet::OStatefulPropertySet()
179 :OPropertyStateHelper( GetBroadcastHelper() )
184 OStatefulPropertySet::~OStatefulPropertySet()
189 Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes() throw(RuntimeException, std::exception)
191 Sequence< Type > aOwnTypes( 2 );
192 aOwnTypes[0] = cppu::UnoType<XWeak>::get();
193 aOwnTypes[1] = cppu::UnoType<XTypeProvider>::get();
195 return concatSequences(
196 aOwnTypes,
197 OPropertyStateHelper::getTypes()
201 Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId() throw(RuntimeException, std::exception)
203 return css::uno::Sequence<sal_Int8>();
207 Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType ) throw(RuntimeException, std::exception)
209 Any aReturn = OWeakObject::queryInterface( _rType );
210 if ( !aReturn.hasValue() )
211 aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
212 if ( !aReturn.hasValue() )
213 aReturn = OPropertyStateHelper::queryInterface( _rType );
214 return aReturn;
218 void SAL_CALL OStatefulPropertySet::acquire() throw()
220 ::cppu::OWeakObject::acquire();
224 void SAL_CALL OStatefulPropertySet::release() throw()
226 ::cppu::OWeakObject::release();
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */