Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <comphelper/propstate.hxx>
21 : #include <com/sun/star/uno/genfunc.h>
22 : #include <cppuhelper/queryinterface.hxx>
23 : #include <comphelper/sequence.hxx>
24 :
25 : namespace comphelper
26 : {
27 :
28 :
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;
39 :
40 :
41 : // OPropertyStateHelper
42 :
43 :
44 :
45 0 : ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::queryInterface(const ::com::sun::star::uno::Type& _rType) throw( ::com::sun::star::uno::RuntimeException, std::exception)
46 : {
47 0 : ::com::sun::star::uno::Any aReturn = OPropertySetHelper2::queryInterface(_rType);
48 : // our own ifaces
49 0 : if ( !aReturn.hasValue() )
50 0 : aReturn = ::cppu::queryInterface(_rType, static_cast< ::com::sun::star::beans::XPropertyState*>(this));
51 :
52 0 : return aReturn;
53 : }
54 :
55 :
56 0 : css::uno::Sequence<css::uno::Type> OPropertyStateHelper::getTypes()
57 : {
58 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> aTypes(4);
59 0 : ::com::sun::star::uno::Type* pTypes = aTypes.getArray();
60 : // base class types
61 0 : pTypes[0] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>*)NULL);
62 0 : pTypes[1] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>*)NULL);
63 0 : pTypes[2] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>*)NULL);
64 : // my own type
65 0 : pTypes[3] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState>*)NULL);
66 0 : return aTypes;
67 : }
68 :
69 0 : OPropertyStateHelper::OPropertyStateHelper(
70 : ::cppu::OBroadcastHelper& rBHlp,
71 : ::cppu::IEventNotificationHook *i_pFireEvents)
72 0 : : ::cppu::OPropertySetHelper2(rBHlp, i_pFireEvents) { }
73 :
74 0 : OPropertyStateHelper::~OPropertyStateHelper() {}
75 :
76 :
77 0 : void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const ::com::sun::star::uno::Any& aNewValue, const ::com::sun::star::uno::Any& aOldValue)
78 : {
79 0 : fire(&nHandle, &aNewValue, &aOldValue, 1, sal_False);
80 0 : }
81 :
82 : // XPropertyState
83 :
84 0 : ::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)
85 : {
86 0 : cppu::IPropertyArrayHelper& rPH = getInfoHelper();
87 0 : sal_Int32 nHandle = rPH.getHandleByName(_rsName);
88 :
89 0 : if (nHandle == -1)
90 0 : throw ::com::sun::star::beans::UnknownPropertyException();
91 :
92 0 : return getPropertyStateByHandle(nHandle);
93 : }
94 :
95 :
96 0 : void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::uno::RuntimeException, std::exception)
97 : {
98 0 : cppu::IPropertyArrayHelper& rPH = getInfoHelper();
99 0 : sal_Int32 nHandle = rPH.getHandleByName(_rsName);
100 :
101 0 : if (nHandle == -1)
102 0 : throw ::com::sun::star::beans::UnknownPropertyException();
103 :
104 0 : setPropertyToDefaultByHandle(nHandle);
105 0 : }
106 :
107 :
108 0 : ::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)
109 : {
110 0 : cppu::IPropertyArrayHelper& rPH = getInfoHelper();
111 0 : sal_Int32 nHandle = rPH.getHandleByName(_rsName);
112 :
113 0 : if (nHandle == -1)
114 0 : throw ::com::sun::star::beans::UnknownPropertyException();
115 :
116 0 : return getPropertyDefaultByHandle(nHandle);
117 : }
118 :
119 :
120 0 : ::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)
121 : {
122 0 : sal_Int32 nLen = _rPropertyNames.getLength();
123 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> aRet(nLen);
124 0 : ::com::sun::star::beans::PropertyState* pValues = aRet.getArray();
125 0 : const OUString* pNames = _rPropertyNames.getConstArray();
126 :
127 0 : cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
128 :
129 0 : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property> aProps = rHelper.getProperties();
130 0 : const ::com::sun::star::beans::Property* pProps = aProps.getConstArray();
131 0 : sal_Int32 nPropCount = aProps.getLength();
132 :
133 0 : osl::MutexGuard aGuard(rBHelper.rMutex);
134 0 : for (sal_Int32 i=0, j=0; i<nPropCount && j<nLen; ++i, ++pProps)
135 : {
136 : // get the values only for valid properties
137 0 : if (pProps->Name.equals(*pNames))
138 : {
139 0 : *pValues = getPropertyState(*pNames);
140 0 : ++pValues;
141 0 : ++pNames;
142 0 : ++j;
143 : }
144 : }
145 :
146 0 : return aRet;
147 : }
148 :
149 :
150 0 : ::com::sun::star::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
151 : {
152 : // simply compare the current and the default value
153 0 : Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
154 0 : Any aDefaultValue; getFastPropertyValue( aDefaultValue, _nHandle );
155 :
156 : bool bEqual = uno_type_equalData(
157 0 : const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
158 0 : const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
159 : reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
160 : reinterpret_cast< uno_ReleaseFunc >(cpp_release)
161 0 : );
162 0 : return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
163 : }
164 :
165 :
166 0 : void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
167 : {
168 0 : setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
169 0 : }
170 :
171 :
172 0 : ::com::sun::star::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
173 : {
174 0 : return ::com::sun::star::uno::Any();
175 : }
176 :
177 :
178 : // OStatefulPropertySet
179 :
180 :
181 0 : OStatefulPropertySet::OStatefulPropertySet()
182 0 : :OPropertyStateHelper( GetBroadcastHelper() )
183 : {
184 0 : }
185 :
186 :
187 0 : OStatefulPropertySet::~OStatefulPropertySet()
188 : {
189 0 : }
190 :
191 :
192 0 : Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes() throw(RuntimeException, std::exception)
193 : {
194 0 : Sequence< Type > aOwnTypes( 2 );
195 0 : aOwnTypes[0] = cppu::UnoType<XWeak>::get();
196 0 : aOwnTypes[1] = cppu::UnoType<XTypeProvider>::get();
197 :
198 : return concatSequences(
199 : aOwnTypes,
200 : OPropertyStateHelper::getTypes()
201 0 : );
202 : }
203 :
204 0 : Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId() throw(RuntimeException, std::exception)
205 : {
206 0 : return css::uno::Sequence<sal_Int8>();
207 : }
208 :
209 :
210 0 : Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType ) throw(RuntimeException, std::exception)
211 : {
212 0 : Any aReturn = OWeakObject::queryInterface( _rType );
213 0 : if ( !aReturn.hasValue() )
214 0 : aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
215 0 : if ( !aReturn.hasValue() )
216 0 : aReturn = OPropertyStateHelper::queryInterface( _rType );
217 0 : return aReturn;
218 : }
219 :
220 :
221 0 : void SAL_CALL OStatefulPropertySet::acquire() throw()
222 : {
223 0 : ::cppu::OWeakObject::acquire();
224 0 : }
225 :
226 :
227 0 : void SAL_CALL OStatefulPropertySet::release() throw()
228 : {
229 0 : ::cppu::OWeakObject::release();
230 0 : }
231 :
232 :
233 : }
234 :
235 :
236 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|