xref: /trunk/main/comphelper/source/property/ChainablePropertySet.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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/ChainablePropertySet.hxx>
31 #include <comphelper/ChainablePropertySetInfo.hxx>
32 #include <vos/mutex.hxx>
33 
34 #include <memory>       // STL auto_ptr
35 
36 
37 using namespace ::rtl;
38 using namespace ::comphelper;
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::beans;
43 using ::vos::IMutex;
44 
45 ChainablePropertySet::ChainablePropertySet( comphelper::ChainablePropertySetInfo* pInfo, vos::IMutex *pMutex )
46     throw()
47 : mpInfo ( pInfo )
48 , mpMutex ( pMutex )
49 , mxInfo ( pInfo )
50 {
51 }
52 
53 ChainablePropertySet::~ChainablePropertySet()
54     throw()
55 {
56 }
57 
58 // XPropertySet
59 Reference< XPropertySetInfo > SAL_CALL ChainablePropertySet::getPropertySetInfo(  )
60     throw(RuntimeException)
61 {
62     return mxInfo;
63 }
64 
65 void ChainablePropertySet::lockMutex()
66 {
67     if (mpMutex)
68         mpMutex->acquire();
69 }
70 
71 void ChainablePropertySet::unlockMutex()
72 {
73     if (mpMutex)
74         mpMutex->release();
75 }
76 
77 void SAL_CALL ChainablePropertySet::setPropertyValue( const ::rtl::OUString& rPropertyName, const Any& rValue )
78     throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
79 {
80     // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
81     std::auto_ptr< vos::OGuard > pMutexGuard;
82     if (mpMutex)
83         pMutexGuard.reset( new vos::OGuard(mpMutex) );
84 
85     PropertyInfoHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
86 
87     if( aIter == mpInfo->maMap.end())
88         throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
89 
90     _preSetValues();
91     _setSingleValue( *((*aIter).second), rValue );
92     _postSetValues();
93 }
94 
95 Any SAL_CALL ChainablePropertySet::getPropertyValue( const ::rtl::OUString& rPropertyName )
96     throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
97 {
98     // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
99     std::auto_ptr< vos::OGuard > pMutexGuard;
100     if (mpMutex)
101         pMutexGuard.reset( new vos::OGuard(mpMutex) );
102 
103     PropertyInfoHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
104 
105     if( aIter == mpInfo->maMap.end())
106         throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
107 
108     Any aAny;
109     _preGetValues ();
110     _getSingleValue( *((*aIter).second), aAny );
111     _postGetValues ();
112 
113     return aAny;
114 }
115 
116 void SAL_CALL ChainablePropertySet::addPropertyChangeListener( const ::rtl::OUString&, const Reference< XPropertyChangeListener >& )
117     throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
118 {
119     // todo
120 }
121 
122 void SAL_CALL ChainablePropertySet::removePropertyChangeListener( const ::rtl::OUString&, const Reference< XPropertyChangeListener >& )
123     throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
124 {
125     // todo
126 }
127 
128 void SAL_CALL ChainablePropertySet::addVetoableChangeListener( const ::rtl::OUString&, const Reference< XVetoableChangeListener >& )
129     throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
130 {
131     // todo
132 }
133 
134 void SAL_CALL ChainablePropertySet::removeVetoableChangeListener( const ::rtl::OUString&, const Reference< XVetoableChangeListener >& )
135     throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
136 {
137     // todo
138 }
139 
140 // XMultiPropertySet
141 void SAL_CALL ChainablePropertySet::setPropertyValues( const Sequence< ::rtl::OUString >& aPropertyNames, const Sequence< Any >& aValues )
142     throw(PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
143 {
144     // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
145     std::auto_ptr< vos::OGuard > pMutexGuard;
146     if (mpMutex)
147         pMutexGuard.reset( new vos::OGuard(mpMutex) );
148 
149     const sal_Int32 nCount = aPropertyNames.getLength();
150 
151     if( nCount != aValues.getLength() )
152         throw IllegalArgumentException();
153 
154     if( nCount )
155     {
156         _preSetValues();
157 
158         const Any * pAny = aValues.getConstArray();
159         const OUString * pString = aPropertyNames.getConstArray();
160         PropertyInfoHash::const_iterator aEnd = mpInfo->maMap.end(), aIter;
161 
162         for ( sal_Int32 i = 0; i < nCount; ++i, ++pString, ++pAny )
163         {
164             aIter = mpInfo->maMap.find ( *pString );
165             if ( aIter == aEnd )
166                 throw UnknownPropertyException( *pString, static_cast< XPropertySet* >( this ) );
167 
168             _setSingleValue ( *((*aIter).second), *pAny );
169         }
170 
171         _postSetValues();
172     }
173 }
174 
175 Sequence< Any > SAL_CALL ChainablePropertySet::getPropertyValues( const Sequence< ::rtl::OUString >& aPropertyNames )
176     throw(RuntimeException)
177 {
178     // acquire mutex in c-tor and releases it in the d-tor (exception safe!).
179     std::auto_ptr< vos::OGuard > pMutexGuard;
180     if (mpMutex)
181         pMutexGuard.reset( new vos::OGuard(mpMutex) );
182 
183     const sal_Int32 nCount = aPropertyNames.getLength();
184 
185     Sequence < Any > aValues ( nCount );
186 
187     if( nCount )
188     {
189         _preGetValues();
190 
191         Any * pAny = aValues.getArray();
192         const OUString * pString = aPropertyNames.getConstArray();
193         PropertyInfoHash::const_iterator aEnd = mpInfo->maMap.end(), aIter;
194 
195         for ( sal_Int32 i = 0; i < nCount; ++i, ++pString, ++pAny )
196         {
197             aIter = mpInfo->maMap.find ( *pString );
198             if ( aIter == aEnd )
199                 throw UnknownPropertyException( *pString, static_cast< XPropertySet* >( this ) );
200 
201             _getSingleValue ( *((*aIter).second), *pAny );
202         }
203 
204         _postGetValues();
205     }
206     return aValues;
207 }
208 
209 void SAL_CALL ChainablePropertySet::addPropertiesChangeListener( const Sequence< ::rtl::OUString >&, const Reference< XPropertiesChangeListener >& )
210     throw(RuntimeException)
211 {
212     // todo
213 }
214 
215 void SAL_CALL ChainablePropertySet::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
216     throw(RuntimeException)
217 {
218     // todo
219 }
220 
221 void SAL_CALL ChainablePropertySet::firePropertiesChangeEvent( const Sequence< ::rtl::OUString >&, const Reference< XPropertiesChangeListener >& )
222     throw(RuntimeException)
223 {
224     // todo
225 }
226 
227 // XPropertyState
228 PropertyState SAL_CALL ChainablePropertySet::getPropertyState( const ::rtl::OUString& PropertyName )
229     throw(UnknownPropertyException, RuntimeException)
230 {
231     PropertyInfoHash::const_iterator aIter =  mpInfo->maMap.find( PropertyName );
232     if( aIter == mpInfo->maMap.end())
233         throw UnknownPropertyException( PropertyName, static_cast< XPropertySet* >( this ) );
234 
235     PropertyState aState;
236 
237     _preGetPropertyState();
238     _getPropertyState( *((*aIter).second), aState );
239     _postGetPropertyState();
240 
241     return aState;
242 }
243 
244 Sequence< PropertyState > SAL_CALL ChainablePropertySet::getPropertyStates( const Sequence< ::rtl::OUString >& rPropertyNames )
245     throw(UnknownPropertyException, RuntimeException)
246 {
247     const sal_Int32 nCount = rPropertyNames.getLength();
248 
249     Sequence< PropertyState > aStates( nCount );
250     if( nCount )
251     {
252         PropertyState * pState = aStates.getArray();
253         const OUString * pString = rPropertyNames.getConstArray();
254         PropertyInfoHash::const_iterator aEnd = mpInfo->maMap.end(), aIter;
255         _preGetPropertyState();
256 
257         for ( sal_Int32 i = 0; i < nCount; ++i, ++pString, ++pState )
258         {
259             aIter = mpInfo->maMap.find ( *pString );
260             if ( aIter == aEnd )
261                 throw UnknownPropertyException( *pString, static_cast< XPropertySet* >( this ) );
262 
263             _getPropertyState ( *((*aIter).second), *pState );
264         }
265         _postGetPropertyState();
266     }
267     return aStates;
268 }
269 
270 void SAL_CALL ChainablePropertySet::setPropertyToDefault( const ::rtl::OUString& rPropertyName )
271     throw(UnknownPropertyException, RuntimeException)
272 {
273     PropertyInfoHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
274 
275     if( aIter == mpInfo->maMap.end())
276         throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
277     _setPropertyToDefault( *((*aIter).second) );
278 }
279 
280 Any SAL_CALL ChainablePropertySet::getPropertyDefault( const ::rtl::OUString& rPropertyName )
281     throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
282 {
283     PropertyInfoHash::const_iterator aIter = mpInfo->maMap.find ( rPropertyName );
284 
285     if( aIter == mpInfo->maMap.end())
286         throw UnknownPropertyException( rPropertyName, static_cast< XPropertySet* >( this ) );
287     return _getPropertyDefault( *((*aIter).second) );
288 }
289 
290 void ChainablePropertySet::_preGetPropertyState ()
291     throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException )
292 {
293     OSL_ENSURE( sal_False, "you have to implement this yourself!");
294 }
295 
296 void ChainablePropertySet::_getPropertyState( const comphelper::PropertyInfo&, PropertyState& )
297     throw(UnknownPropertyException )
298 {
299     OSL_ENSURE( sal_False, "you have to implement this yourself!");
300 }
301 
302 void ChainablePropertySet::_postGetPropertyState ()
303     throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException )
304 {
305     OSL_ENSURE( sal_False, "you have to implement this yourself!");
306 }
307 
308 void ChainablePropertySet::_setPropertyToDefault( const comphelper::PropertyInfo& )
309     throw(UnknownPropertyException )
310 {
311     OSL_ENSURE( sal_False, "you have to implement this yourself!");
312 }
313 
314 Any ChainablePropertySet::_getPropertyDefault( const comphelper::PropertyInfo& )
315     throw(UnknownPropertyException, WrappedTargetException )
316 {
317     OSL_ENSURE( sal_False, "you have to implement this yourself!");
318 
319     Any aAny;
320     return aAny;
321 }
322