1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 #include <comphelper/propstate.hxx>
27 #include <com/sun/star/uno/genfunc.h>
28 #include <cppuhelper/queryinterface.hxx>
29 #include <comphelper/sequence.hxx>
30 
31 //.........................................................................
32 namespace comphelper
33 {
34 //.........................................................................
35 
36     using ::com::sun::star::uno::Reference;
37     using ::com::sun::star::uno::Type;
38     using ::com::sun::star::uno::RuntimeException;
39     using ::com::sun::star::uno::Sequence;
40     using ::com::sun::star::lang::XTypeProvider;
41     using ::com::sun::star::uno::Any;
42     using ::com::sun::star::uno::cpp_queryInterface;
43     using ::com::sun::star::uno::cpp_release;
44     using ::com::sun::star::beans::PropertyState_DEFAULT_VALUE;
45     using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
46 
47     //=====================================================================
48     // OPropertyStateHelper
49     //=====================================================================
50 
51     //---------------------------------------------------------------------
queryInterface(const::com::sun::star::uno::Type & _rType)52     ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::queryInterface(const  ::com::sun::star::uno::Type& _rType) throw( ::com::sun::star::uno::RuntimeException)
53     {
54 	    ::com::sun::star::uno::Any aReturn = OPropertySetHelper::queryInterface(_rType);
55 	    // our own ifaces
56 	    if ( !aReturn.hasValue() )
57 		    aReturn = ::cppu::queryInterface(_rType, static_cast< ::com::sun::star::beans::XPropertyState*>(this));
58 
59 	    return aReturn;
60     }
61 
62     //---------------------------------------------------------------------
getTypes()63     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> OPropertyStateHelper::getTypes() throw( ::com::sun::star::uno::RuntimeException)
64     {
65 	    static  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> aTypes;
66 	    if (!aTypes.getLength())
67 	    {
68 		    aTypes.realloc(4);
69 		    ::com::sun::star::uno::Type* pTypes = aTypes.getArray();
70 		    // base class types
71 		    pTypes[0] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>*)NULL);
72 		    pTypes[1] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>*)NULL);
73 		    pTypes[2] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>*)NULL);
74 		    // my own type
75 		    pTypes[3] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState>*)NULL);
76 	    }
77 	    return aTypes;
78     }
79 
OPropertyStateHelper(::cppu::OBroadcastHelper & rBHlp,::cppu::IEventNotificationHook * i_pFireEvents)80 	OPropertyStateHelper::OPropertyStateHelper(
81         ::cppu::OBroadcastHelper& rBHlp,
82         ::cppu::IEventNotificationHook *i_pFireEvents)
83         :   ::cppu::OPropertySetHelper(rBHlp, i_pFireEvents) { }
84 
~OPropertyStateHelper()85     OPropertyStateHelper::~OPropertyStateHelper() {}
86 
87     //---------------------------------------------------------------------
firePropertyChange(sal_Int32 nHandle,const::com::sun::star::uno::Any & aNewValue,const::com::sun::star::uno::Any & aOldValue)88     void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const  ::com::sun::star::uno::Any& aNewValue, const  ::com::sun::star::uno::Any& aOldValue)
89     {
90 	    fire(&nHandle, &aNewValue, &aOldValue, 1, sal_False);
91     }
92 
93     // XPropertyState
94     //---------------------------------------------------------------------
getPropertyState(const::rtl::OUString & _rsName)95     ::com::sun::star::beans::PropertyState SAL_CALL OPropertyStateHelper::getPropertyState(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException,  ::com::sun::star::uno::RuntimeException)
96     {
97 	    cppu::IPropertyArrayHelper& rPH = getInfoHelper();
98 	    sal_Int32 nHandle = rPH.getHandleByName(_rsName);
99 
100 	    if (nHandle == -1)
101 		    throw  ::com::sun::star::beans::UnknownPropertyException();
102 
103 	    return getPropertyStateByHandle(nHandle);
104     }
105 
106     //---------------------------------------------------------------------
setPropertyToDefault(const::rtl::OUString & _rsName)107     void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException,  ::com::sun::star::uno::RuntimeException)
108     {
109 	    cppu::IPropertyArrayHelper& rPH = getInfoHelper();
110 	    sal_Int32 nHandle = rPH.getHandleByName(_rsName);
111 
112 	    if (nHandle == -1)
113 		    throw  ::com::sun::star::beans::UnknownPropertyException();
114 
115 	    setPropertyToDefaultByHandle(nHandle);
116     }
117 
118     //---------------------------------------------------------------------
getPropertyDefault(const::rtl::OUString & _rsName)119     ::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)
120     {
121 	    cppu::IPropertyArrayHelper& rPH = getInfoHelper();
122 	    sal_Int32 nHandle = rPH.getHandleByName(_rsName);
123 
124 	    if (nHandle == -1)
125 		    throw  ::com::sun::star::beans::UnknownPropertyException();
126 
127 	    return getPropertyDefaultByHandle(nHandle);
128     }
129 
130     //---------------------------------------------------------------------
getPropertyStates(const::com::sun::star::uno::Sequence<::rtl::OUString> & _rPropertyNames)131     ::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)
132     {
133 	    sal_Int32 nLen = _rPropertyNames.getLength();
134 	    ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> aRet(nLen);
135 	    ::com::sun::star::beans::PropertyState* pValues = aRet.getArray();
136 	    const ::rtl::OUString* pNames = _rPropertyNames.getConstArray();
137 
138 	    cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
139 
140 	    ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property> aProps = rHelper.getProperties();
141 	    const  ::com::sun::star::beans::Property* pProps = aProps.getConstArray();
142 	    sal_Int32 nPropCount	   = aProps.getLength();
143 
144 	    osl::MutexGuard aGuard(rBHelper.rMutex);
145 	    for (sal_Int32 i=0, j=0; i<nPropCount && j<nLen; ++i, ++pProps)
146 	    {
147 		    // get the values only for valid properties
148 		    if (pProps->Name.equals(*pNames))
149 		    {
150 			    *pValues = getPropertyState(*pNames);
151 			    ++pValues;
152 			    ++pNames;
153 			    ++j;
154 		    }
155 	    }
156 
157 	    return aRet;
158     }
159 
160     //---------------------------------------------------------------------
getPropertyStateByHandle(sal_Int32 _nHandle)161     ::com::sun::star::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
162     {
163 	    // simply compare the current and the default value
164 	    Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
165 	    Any aDefaultValue;  getFastPropertyValue( aDefaultValue, _nHandle );
166 
167 	    sal_Bool bEqual = uno_type_equalData(
168 			    const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
169 			    const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
170 			    reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
171                 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
172 		    );
173         return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
174     }
175 
176     //---------------------------------------------------------------------
setPropertyToDefaultByHandle(sal_Int32 _nHandle)177     void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
178     {
179         setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
180     }
181 
182     //---------------------------------------------------------------------
getPropertyDefaultByHandle(sal_Int32) const183     ::com::sun::star::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
184     {
185 	    return  ::com::sun::star::uno::Any();
186     }
187 
188     //=====================================================================
189     // OStatefulPropertySet
190     //=====================================================================
191     //---------------------------------------------------------------------
OStatefulPropertySet()192     OStatefulPropertySet::OStatefulPropertySet()
193         :OPropertyStateHelper( GetBroadcastHelper() )
194     {
195     }
196 
197     //---------------------------------------------------------------------
~OStatefulPropertySet()198     OStatefulPropertySet::~OStatefulPropertySet()
199     {
200     }
201 
202     //---------------------------------------------------------------------
getTypes()203     Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes() throw(RuntimeException)
204     {
205         Sequence< Type > aOwnTypes( 2 );
206         aOwnTypes[0] = XWeak::static_type();
207         aOwnTypes[1] = XTypeProvider::static_type();
208 
209         return concatSequences(
210             aOwnTypes,
211             OPropertyStateHelper::getTypes()
212         );
213     }
214 
215     //---------------------------------------------------------------------
getImplementationId()216     Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId() throw(RuntimeException)
217     {
218         static ::cppu::OImplementationId * pId = NULL;
219         if ( !pId )
220         {
221             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
222             if ( !pId )
223             {
224                 static ::cppu::OImplementationId aId;
225                 pId = &aId;
226             }
227         }
228         return pId->getImplementationId();
229     }
230 
231     //---------------------------------------------------------------------
queryInterface(const Type & _rType)232     Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType ) throw(RuntimeException)
233     {
234         Any aReturn = OWeakObject::queryInterface( _rType );
235         if ( !aReturn.hasValue() )
236             aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
237         if ( !aReturn.hasValue() )
238             aReturn = OPropertyStateHelper::queryInterface( _rType );
239         return aReturn;
240     }
241 
242     //---------------------------------------------------------------------
acquire()243     void SAL_CALL OStatefulPropertySet::acquire() throw()
244     {
245         ::cppu::OWeakObject::acquire();
246     }
247 
248     //---------------------------------------------------------------------
release()249     void SAL_CALL OStatefulPropertySet::release() throw()
250     {
251         ::cppu::OWeakObject::release();
252     }
253 
254 //.........................................................................
255 }
256 //.........................................................................
257 
258