1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propstate.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/propstate.hxx>
34 #include <com/sun/star/uno/genfunc.h>
35 #include <cppuhelper/queryinterface.hxx>
36 #include <comphelper/sequence.hxx>
38 //.........................................................................
41 //.........................................................................
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::Type
;
45 using ::com::sun::star::uno::RuntimeException
;
46 using ::com::sun::star::uno::Sequence
;
47 using ::com::sun::star::lang::XTypeProvider
;
48 using ::com::sun::star::uno::Any
;
49 using ::com::sun::star::uno::cpp_queryInterface
;
50 using ::com::sun::star::uno::cpp_release
;
51 using ::com::sun::star::beans::PropertyState_DEFAULT_VALUE
;
52 using ::com::sun::star::beans::PropertyState_DIRECT_VALUE
;
54 //=====================================================================
55 // OPropertyStateHelper
56 //=====================================================================
58 //---------------------------------------------------------------------
59 ::com::sun::star::uno::Any SAL_CALL
OPropertyStateHelper::queryInterface(const ::com::sun::star::uno::Type
& _rType
) throw( ::com::sun::star::uno::RuntimeException
)
61 ::com::sun::star::uno::Any aReturn
= OPropertySetHelper::queryInterface(_rType
);
63 if ( !aReturn
.hasValue() )
64 aReturn
= ::cppu::queryInterface(_rType
, static_cast< ::com::sun::star::beans::XPropertyState
*>(this));
69 //---------------------------------------------------------------------
70 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> OPropertyStateHelper::getTypes() throw( ::com::sun::star::uno::RuntimeException
)
72 static ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> aTypes
;
73 if (!aTypes
.getLength())
76 ::com::sun::star::uno::Type
* pTypes
= aTypes
.getArray();
78 pTypes
[0] = getCppuType(( ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>*)NULL
);
79 pTypes
[1] = getCppuType(( ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XMultiPropertySet
>*)NULL
);
80 pTypes
[2] = getCppuType(( ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XFastPropertySet
>*)NULL
);
82 pTypes
[3] = getCppuType(( ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertyState
>*)NULL
);
87 OPropertyStateHelper::OPropertyStateHelper(
88 ::cppu::OBroadcastHelper
& rBHlp
,
89 ::cppu::IEventNotificationHook
*i_pFireEvents
)
90 : ::cppu::OPropertySetHelper(rBHlp
, i_pFireEvents
) { }
92 OPropertyStateHelper::~OPropertyStateHelper() {}
94 //---------------------------------------------------------------------
95 void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle
, const ::com::sun::star::uno::Any
& aNewValue
, const ::com::sun::star::uno::Any
& aOldValue
)
97 fire(&nHandle
, &aNewValue
, &aOldValue
, 1, sal_False
);
101 //---------------------------------------------------------------------
102 ::com::sun::star::beans::PropertyState SAL_CALL
OPropertyStateHelper::getPropertyState(const ::rtl::OUString
& _rsName
) throw( ::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::uno::RuntimeException
)
104 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
105 sal_Int32 nHandle
= rPH
.getHandleByName(_rsName
);
108 throw ::com::sun::star::beans::UnknownPropertyException();
110 return getPropertyStateByHandle(nHandle
);
113 //---------------------------------------------------------------------
114 void SAL_CALL
OPropertyStateHelper::setPropertyToDefault(const ::rtl::OUString
& _rsName
) throw( ::com::sun::star::beans::UnknownPropertyException
, ::com::sun::star::uno::RuntimeException
)
116 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
117 sal_Int32 nHandle
= rPH
.getHandleByName(_rsName
);
120 throw ::com::sun::star::beans::UnknownPropertyException();
122 setPropertyToDefaultByHandle(nHandle
);
125 //---------------------------------------------------------------------
126 ::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
)
128 cppu::IPropertyArrayHelper
& rPH
= getInfoHelper();
129 sal_Int32 nHandle
= rPH
.getHandleByName(_rsName
);
132 throw ::com::sun::star::beans::UnknownPropertyException();
134 return getPropertyDefaultByHandle(nHandle
);
137 //---------------------------------------------------------------------
138 ::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
)
140 sal_Int32 nLen
= _rPropertyNames
.getLength();
141 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyState
> aRet(nLen
);
142 ::com::sun::star::beans::PropertyState
* pValues
= aRet
.getArray();
143 const ::rtl::OUString
* pNames
= _rPropertyNames
.getConstArray();
145 cppu::IPropertyArrayHelper
& rHelper
= getInfoHelper();
147 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::Property
> aProps
= rHelper
.getProperties();
148 const ::com::sun::star::beans::Property
* pProps
= aProps
.getConstArray();
149 sal_Int32 nPropCount
= aProps
.getLength();
151 osl::MutexGuard
aGuard(rBHelper
.rMutex
);
152 for (sal_Int32 i
=0, j
=0; i
<nPropCount
&& j
<nLen
; ++i
, ++pProps
)
154 // get the values only for valid properties
155 if (pProps
->Name
.equals(*pNames
))
157 *pValues
= getPropertyState(*pNames
);
167 //---------------------------------------------------------------------
168 ::com::sun::star::beans::PropertyState
OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle
)
170 // simply compare the current and the default value
171 Any aCurrentValue
= getPropertyDefaultByHandle( _nHandle
);
172 Any aDefaultValue
; getFastPropertyValue( aDefaultValue
, _nHandle
);
174 sal_Bool bEqual
= uno_type_equalData(
175 const_cast< void* >( aCurrentValue
.getValue() ), aCurrentValue
.getValueType().getTypeLibType(),
176 const_cast< void* >( aDefaultValue
.getValue() ), aDefaultValue
.getValueType().getTypeLibType(),
177 reinterpret_cast< uno_QueryInterfaceFunc
>(cpp_queryInterface
),
178 reinterpret_cast< uno_ReleaseFunc
>(cpp_release
)
180 return bEqual
? PropertyState_DEFAULT_VALUE
: PropertyState_DIRECT_VALUE
;
183 //---------------------------------------------------------------------
184 void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle
)
186 setFastPropertyValue( _nHandle
, getPropertyDefaultByHandle( _nHandle
) );
189 //---------------------------------------------------------------------
190 ::com::sun::star::uno::Any
OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32
) const
192 return ::com::sun::star::uno::Any();
195 //=====================================================================
196 // OStatefulPropertySet
197 //=====================================================================
198 //---------------------------------------------------------------------
199 OStatefulPropertySet::OStatefulPropertySet()
200 :OPropertyStateHelper( GetBroadcastHelper() )
204 //---------------------------------------------------------------------
205 OStatefulPropertySet::~OStatefulPropertySet()
209 //---------------------------------------------------------------------
210 Sequence
< Type
> SAL_CALL
OStatefulPropertySet::getTypes() throw(RuntimeException
)
212 Sequence
< Type
> aOwnTypes( 2 );
213 aOwnTypes
[0] = XWeak::static_type();
214 aOwnTypes
[1] = XTypeProvider::static_type();
216 return concatSequences(
218 OPropertyStateHelper::getTypes()
222 //---------------------------------------------------------------------
223 Sequence
< sal_Int8
> SAL_CALL
OStatefulPropertySet::getImplementationId() throw(RuntimeException
)
225 static ::cppu::OImplementationId
* pId
= NULL
;
228 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
231 static ::cppu::OImplementationId aId
;
235 return pId
->getImplementationId();
238 //---------------------------------------------------------------------
239 Any SAL_CALL
OStatefulPropertySet::queryInterface( const Type
& _rType
) throw(RuntimeException
)
241 Any aReturn
= OWeakObject::queryInterface( _rType
);
242 if ( !aReturn
.hasValue() )
243 aReturn
= ::cppu::queryInterface( _rType
, static_cast< XTypeProvider
* >( this ) );
244 if ( !aReturn
.hasValue() )
245 aReturn
= OPropertyStateHelper::queryInterface( _rType
);
249 //---------------------------------------------------------------------
250 void SAL_CALL
OStatefulPropertySet::acquire() throw()
252 ::cppu::OWeakObject::acquire();
255 //---------------------------------------------------------------------
256 void SAL_CALL
OStatefulPropertySet::release() throw()
258 ::cppu::OWeakObject::release();
261 //.........................................................................
263 //.........................................................................