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