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