1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
30 #include <comphelper/propstate.hxx>
31 #include <com/sun/star/uno/genfunc.h>
32 #include <cppuhelper/queryinterface.hxx>
33 #include <comphelper/sequence.hxx>
34 
35 //.........................................................................
36 namespace comphelper
37 {
38 //.........................................................................
39 
40     using ::com::sun::star::uno::Reference;
41     using ::com::sun::star::uno::Type;
42     using ::com::sun::star::uno::RuntimeException;
43     using ::com::sun::star::uno::Sequence;
44     using ::com::sun::star::lang::XTypeProvider;
45     using ::com::sun::star::uno::Any;
46     using ::com::sun::star::uno::cpp_queryInterface;
47     using ::com::sun::star::uno::cpp_release;
48     using ::com::sun::star::beans::PropertyState_DEFAULT_VALUE;
49     using ::com::sun::star::beans::PropertyState_DIRECT_VALUE;
50 
51     //=====================================================================
52     // OPropertyStateHelper
53     //=====================================================================
54 
55     //---------------------------------------------------------------------
56     ::com::sun::star::uno::Any SAL_CALL OPropertyStateHelper::queryInterface(const  ::com::sun::star::uno::Type& _rType) throw( ::com::sun::star::uno::RuntimeException)
57     {
58 	    ::com::sun::star::uno::Any aReturn = OPropertySetHelper::queryInterface(_rType);
59 	    // our own ifaces
60 	    if ( !aReturn.hasValue() )
61 		    aReturn = ::cppu::queryInterface(_rType, static_cast< ::com::sun::star::beans::XPropertyState*>(this));
62 
63 	    return aReturn;
64     }
65 
66     //---------------------------------------------------------------------
67     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> OPropertyStateHelper::getTypes() throw( ::com::sun::star::uno::RuntimeException)
68     {
69 	    static  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type> aTypes;
70 	    if (!aTypes.getLength())
71 	    {
72 		    aTypes.realloc(4);
73 		    ::com::sun::star::uno::Type* pTypes = aTypes.getArray();
74 		    // base class types
75 		    pTypes[0] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>*)NULL);
76 		    pTypes[1] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XMultiPropertySet>*)NULL);
77 		    pTypes[2] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XFastPropertySet>*)NULL);
78 		    // my own type
79 		    pTypes[3] = getCppuType(( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyState>*)NULL);
80 	    }
81 	    return aTypes;
82     }
83 
84 	OPropertyStateHelper::OPropertyStateHelper(
85         ::cppu::OBroadcastHelper& rBHlp,
86         ::cppu::IEventNotificationHook *i_pFireEvents)
87         :   ::cppu::OPropertySetHelper(rBHlp, i_pFireEvents) { }
88 
89     OPropertyStateHelper::~OPropertyStateHelper() {}
90 
91     //---------------------------------------------------------------------
92     void OPropertyStateHelper::firePropertyChange(sal_Int32 nHandle, const  ::com::sun::star::uno::Any& aNewValue, const  ::com::sun::star::uno::Any& aOldValue)
93     {
94 	    fire(&nHandle, &aNewValue, &aOldValue, 1, sal_False);
95     }
96 
97     // XPropertyState
98     //---------------------------------------------------------------------
99     ::com::sun::star::beans::PropertyState SAL_CALL OPropertyStateHelper::getPropertyState(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException,  ::com::sun::star::uno::RuntimeException)
100     {
101 	    cppu::IPropertyArrayHelper& rPH = getInfoHelper();
102 	    sal_Int32 nHandle = rPH.getHandleByName(_rsName);
103 
104 	    if (nHandle == -1)
105 		    throw  ::com::sun::star::beans::UnknownPropertyException();
106 
107 	    return getPropertyStateByHandle(nHandle);
108     }
109 
110     //---------------------------------------------------------------------
111     void SAL_CALL OPropertyStateHelper::setPropertyToDefault(const ::rtl::OUString& _rsName) throw( ::com::sun::star::beans::UnknownPropertyException,  ::com::sun::star::uno::RuntimeException)
112     {
113 	    cppu::IPropertyArrayHelper& rPH = getInfoHelper();
114 	    sal_Int32 nHandle = rPH.getHandleByName(_rsName);
115 
116 	    if (nHandle == -1)
117 		    throw  ::com::sun::star::beans::UnknownPropertyException();
118 
119 	    setPropertyToDefaultByHandle(nHandle);
120     }
121 
122     //---------------------------------------------------------------------
123     ::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)
124     {
125 	    cppu::IPropertyArrayHelper& rPH = getInfoHelper();
126 	    sal_Int32 nHandle = rPH.getHandleByName(_rsName);
127 
128 	    if (nHandle == -1)
129 		    throw  ::com::sun::star::beans::UnknownPropertyException();
130 
131 	    return getPropertyDefaultByHandle(nHandle);
132     }
133 
134     //---------------------------------------------------------------------
135     ::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)
136     {
137 	    sal_Int32 nLen = _rPropertyNames.getLength();
138 	    ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyState> aRet(nLen);
139 	    ::com::sun::star::beans::PropertyState* pValues = aRet.getArray();
140 	    const ::rtl::OUString* pNames = _rPropertyNames.getConstArray();
141 
142 	    cppu::IPropertyArrayHelper& rHelper = getInfoHelper();
143 
144 	    ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property> aProps = rHelper.getProperties();
145 	    const  ::com::sun::star::beans::Property* pProps = aProps.getConstArray();
146 	    sal_Int32 nPropCount	   = aProps.getLength();
147 
148 	    osl::MutexGuard aGuard(rBHelper.rMutex);
149 	    for (sal_Int32 i=0, j=0; i<nPropCount && j<nLen; ++i, ++pProps)
150 	    {
151 		    // get the values only for valid properties
152 		    if (pProps->Name.equals(*pNames))
153 		    {
154 			    *pValues = getPropertyState(*pNames);
155 			    ++pValues;
156 			    ++pNames;
157 			    ++j;
158 		    }
159 	    }
160 
161 	    return aRet;
162     }
163 
164     //---------------------------------------------------------------------
165     ::com::sun::star::beans::PropertyState OPropertyStateHelper::getPropertyStateByHandle( sal_Int32 _nHandle )
166     {
167 	    // simply compare the current and the default value
168 	    Any aCurrentValue = getPropertyDefaultByHandle( _nHandle );
169 	    Any aDefaultValue;  getFastPropertyValue( aDefaultValue, _nHandle );
170 
171 	    sal_Bool bEqual = uno_type_equalData(
172 			    const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(),
173 			    const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(),
174 			    reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface),
175                 reinterpret_cast< uno_ReleaseFunc >(cpp_release)
176 		    );
177         return bEqual ? PropertyState_DEFAULT_VALUE : PropertyState_DIRECT_VALUE;
178     }
179 
180     //---------------------------------------------------------------------
181     void OPropertyStateHelper::setPropertyToDefaultByHandle( sal_Int32 _nHandle )
182     {
183         setFastPropertyValue( _nHandle, getPropertyDefaultByHandle( _nHandle ) );
184     }
185 
186     //---------------------------------------------------------------------
187     ::com::sun::star::uno::Any OPropertyStateHelper::getPropertyDefaultByHandle( sal_Int32 ) const
188     {
189 	    return  ::com::sun::star::uno::Any();
190     }
191 
192     //=====================================================================
193     // OStatefulPropertySet
194     //=====================================================================
195     //---------------------------------------------------------------------
196     OStatefulPropertySet::OStatefulPropertySet()
197         :OPropertyStateHelper( GetBroadcastHelper() )
198     {
199     }
200 
201     //---------------------------------------------------------------------
202     OStatefulPropertySet::~OStatefulPropertySet()
203     {
204     }
205 
206     //---------------------------------------------------------------------
207     Sequence< Type > SAL_CALL OStatefulPropertySet::getTypes() throw(RuntimeException)
208     {
209         Sequence< Type > aOwnTypes( 2 );
210         aOwnTypes[0] = XWeak::static_type();
211         aOwnTypes[1] = XTypeProvider::static_type();
212 
213         return concatSequences(
214             aOwnTypes,
215             OPropertyStateHelper::getTypes()
216         );
217     }
218 
219     //---------------------------------------------------------------------
220     Sequence< sal_Int8 > SAL_CALL OStatefulPropertySet::getImplementationId() throw(RuntimeException)
221     {
222         static ::cppu::OImplementationId * pId = NULL;
223         if ( !pId )
224         {
225             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
226             if ( !pId )
227             {
228                 static ::cppu::OImplementationId aId;
229                 pId = &aId;
230             }
231         }
232         return pId->getImplementationId();
233     }
234 
235     //---------------------------------------------------------------------
236     Any SAL_CALL OStatefulPropertySet::queryInterface( const Type& _rType ) throw(RuntimeException)
237     {
238         Any aReturn = OWeakObject::queryInterface( _rType );
239         if ( !aReturn.hasValue() )
240             aReturn = ::cppu::queryInterface( _rType, static_cast< XTypeProvider* >( this ) );
241         if ( !aReturn.hasValue() )
242             aReturn = OPropertyStateHelper::queryInterface( _rType );
243         return aReturn;
244     }
245 
246     //---------------------------------------------------------------------
247     void SAL_CALL OStatefulPropertySet::acquire() throw()
248     {
249         ::cppu::OWeakObject::acquire();
250     }
251 
252     //---------------------------------------------------------------------
253     void SAL_CALL OStatefulPropertySet::release() throw()
254     {
255         ::cppu::OWeakObject::release();
256     }
257 
258 //.........................................................................
259 }
260 //.........................................................................
261 
262