Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / comphelper / source / property / propstate.cxx
blobdbc36ce2c41cc3eaf8fd46ba2f661aa5dd7fe292
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>
24 #include <rtl/instance.hxx>
26 //.........................................................................
27 namespace comphelper
29 //.........................................................................
31 using ::com::sun::star::uno::Reference;
32 using ::com::sun::star::uno::Type;
33 using ::com::sun::star::uno::RuntimeException;
34 using ::com::sun::star::uno::Sequence;
35 using ::com::sun::star::lang::XTypeProvider;
36 using ::com::sun::star::uno::Any;
37 using ::com::sun::star::uno::cpp_queryInterface;
38 using ::com::sun::star::uno::cpp_release;
39 using ::com::sun::star::beans::PropertyState_DEFAULT_VALUE;
40 using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
42 //=====================================================================
43 // OPropertyStateHelper
44 //=====================================================================
46 //---------------------------------------------------------------------
47 ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::queryInterface(const ::com::sun::star::uno::Type& _rType) throw( ::com::sun::star::uno::RuntimeException)
49 ::com::sun::star::uno::Any aReturn = OPropertySetHelper2::queryInterface(_rType);
50 // our own ifaces
51 if ( !aReturn.hasValue() )
52 aReturn = ::cppu::queryInterface(_rType, static_cast< ::com::sun::star::beans::XPropertyState*>(this));
54 return aReturn;
57 //---------------------------------------------------------------------
58 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> OPropertyStateHelper::getTypes() throw( ::com::sun::star::uno::RuntimeException)
60 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> aTypes(4);
61 ::com::sun::star::uno::Type* pTypes = aTypes.getArray();
62 // base class types
63 pTypes[0] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>*)NULL);
64 pTypes[1] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>*)NULL);
65 pTypes[2] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>*)NULL);
66 // my own type
67 pTypes[3] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState>*)NULL);
68 return aTypes;
71 OPropertyStateHelper::OPropertyStateHelper(
72 ::cppu::OBroadcastHelper& rBHlp,
73 ::cppu::IEventNotificationHook *i_pFireEvents)
74 : ::cppu::OPropertySetHelper2(rBHlp, i_pFireEvents) { }
76 OPropertyStateHelper::~OPropertyStateHelper() {}
78 //---------------------------------------------------------------------
79 void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const ::com::sun::star::uno::Any& aNewValue, const ::com::sun::star::uno::Any& aOldValue)
81 fire(&nHandle, &aNewValue, &aOldValue, 1, sal_False);
84 // XPropertyState
85 //---------------------------------------------------------------------
86 ::com::sun::star::beans::PropertyState SAL_CALL OPropertyStateHelper::getPropertyState(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException)
88 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
89 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
91 if (nHandle == -1)
92 throw ::com::sun::star::beans::UnknownPropertyException();
94 return getPropertyStateByHandle(nHandle);
97 //---------------------------------------------------------------------
98 void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException)
100 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
101 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
103 if (nHandle == -1)
104 throw ::com::sun::star::beans::UnknownPropertyException();
106 setPropertyToDefaultByHandle(nHandle);
109 //---------------------------------------------------------------------
110 ::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)
112 cppu::IPropertyArrayHelper& rPH = getInfoHelper();
113 sal_Int32 nHandle = rPH.getHandleByName(_rsName);
115 if (nHandle == -1)
116 throw ::com::sun::star::beans::UnknownPropertyException();
118 return getPropertyDefaultByHandle(nHandle);
121 //---------------------------------------------------------------------
122 ::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)
124 sal_Int32 nLen = _rPropertyNames.getLength();
125 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> aRet(nLen);
126 ::com::sun::star::beans::PropertyState* pValues = aRet.getArray();
127 const ::rtl::OUString* pNames = _rPropertyNames.getConstArray();
129 cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
131 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property> aProps = rHelper.getProperties();
132 const ::com::sun::star::beans::Property* pProps = aProps.getConstArray();
133 sal_Int32 nPropCount = aProps.getLength();
135 osl::MutexGuard aGuard(rBHelper.rMutex);
136 for (sal_Int32 i=0, j=0; i<nPropCount && j<nLen; ++i, ++pProps)
138 // get the values only for valid properties
139 if (pProps->Name.equals(*pNames))
141 *pValues = getPropertyState(*pNames);
142 ++pValues;
143 ++pNames;
144 ++j;
148 return aRet;
151 //---------------------------------------------------------------------
152 ::com::sun::star::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
154 // simply compare the current and the default value
155 Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
156 Any aDefaultValue; getFastPropertyValue( aDefaultValue, _nHandle );
158 sal_Bool bEqual = uno_type_equalData(
159 const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
160 const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
161 reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
162 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
164 return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
167 //---------------------------------------------------------------------
168 void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
170 setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
173 //---------------------------------------------------------------------
174 ::com::sun::star::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
176 return ::com::sun::star::uno::Any();
179 //=====================================================================
180 // OStatefulPropertySet
181 //=====================================================================
182 //---------------------------------------------------------------------
183 OStatefulPropertySet::OStatefulPropertySet()
184 :OPropertyStateHelper( GetBroadcastHelper() )
188 //---------------------------------------------------------------------
189 OStatefulPropertySet::~OStatefulPropertySet()
193 //---------------------------------------------------------------------
194 Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes() throw(RuntimeException)
196 Sequence< Type > aOwnTypes( 2 );
197 aOwnTypes[0] = XWeak::static_type();
198 aOwnTypes[1] = XTypeProvider::static_type();
200 return concatSequences(
201 aOwnTypes,
202 OPropertyStateHelper::getTypes()
206 namespace { struct lcl_ImplId : public rtl::Static< ::cppu::OImplementationId, lcl_ImplId > {}; }
208 //---------------------------------------------------------------------
209 Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId() throw(RuntimeException)
211 ::cppu::OImplementationId &rID = lcl_ImplId::get();
212 return rID.getImplementationId();
215 //---------------------------------------------------------------------
216 Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType ) throw(RuntimeException)
218 Any aReturn = OWeakObject::queryInterface( _rType );
219 if ( !aReturn.hasValue() )
220 aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
221 if ( !aReturn.hasValue() )
222 aReturn = OPropertyStateHelper::queryInterface( _rType );
223 return aReturn;
226 //---------------------------------------------------------------------
227 void SAL_CALL OStatefulPropertySet::acquire() throw()
229 ::cppu::OWeakObject::acquire();
232 //---------------------------------------------------------------------
233 void SAL_CALL OStatefulPropertySet::release() throw()
235 ::cppu::OWeakObject::release();
238 //.........................................................................
240 //.........................................................................
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */