xref: /trunk/main/basic/source/classes/propacc.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_basic.hxx"
30 
31 #include "propacc.hxx"
32 
33 #include <tools/urlobj.hxx>
34 #include <tools/errcode.hxx>
35 #include <svl/svarray.hxx>
36 #include <basic/sbstar.hxx>
37 #include <sbunoobj.hxx>
38 
39 using com::sun::star::uno::Reference;
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::beans;
43 using namespace cppu;
44 
45 
46 //========================================================================
47 
48 // Declaration conversion from Sbx to UNO with known target type
49 Any sbxToUnoValue( SbxVariable* pVar, const Type& rType, Property* pUnoProperty = NULL );
50 
51 //========================================================================
52 
53 #ifdef WNT
54 #define CDECL _cdecl
55 #endif
56 #if defined(UNX) || defined(OS2)
57 #define CDECL
58 #endif
59 
60 int CDECL SbCompare_PropertyValues_Impl( const void *arg1, const void *arg2 )
61 {
62    return ((PropertyValue*)arg1)->Name.compareTo( ((PropertyValue*)arg2)->Name );
63 }
64 
65 extern "C" int CDECL SbCompare_UString_PropertyValue_Impl( const void *arg1, const void *arg2 )
66 {
67     const ::rtl::OUString *pArg1 = (::rtl::OUString*) arg1;
68     const PropertyValue **pArg2 = (const PropertyValue**) arg2;
69     return pArg1->compareTo( (*pArg2)->Name );
70 }
71 
72 int CDECL SbCompare_Properties_Impl( const void *arg1, const void *arg2 )
73 {
74    return ((Property*)arg1)->Name.compareTo( ((Property*)arg2)->Name );
75 }
76 
77 extern "C" int CDECL SbCompare_UString_Property_Impl( const void *arg1, const void *arg2 )
78 {
79     const ::rtl::OUString *pArg1 = (::rtl::OUString*) arg1;
80     const Property *pArg2 = (Property*) arg2;
81     return pArg1->compareTo( pArg2->Name );
82 }
83 
84 //----------------------------------------------------------------------------
85 
86 SbPropertyValues::SbPropertyValues()
87 {
88 }
89 
90 //----------------------------------------------------------------------------
91 
92 SbPropertyValues::~SbPropertyValues()
93 {
94     _xInfo = Reference< XPropertySetInfo >();
95 
96     for ( sal_uInt16 n = 0; n < _aPropVals.Count(); ++n )
97         delete _aPropVals.GetObject( n );
98 }
99 
100 //----------------------------------------------------------------------------
101 
102 Reference< XPropertySetInfo > SbPropertyValues::getPropertySetInfo(void) throw( RuntimeException )
103 {
104     // create on demand?
105     if ( !_xInfo.is() )
106     {
107         SbPropertySetInfo *pInfo = new SbPropertySetInfo( _aPropVals );
108         ((SbPropertyValues*)this)->_xInfo = (XPropertySetInfo*)pInfo;
109     }
110     return _xInfo;
111 }
112 
113 //-------------------------------------------------------------------------
114 
115 sal_Int32 SbPropertyValues::GetIndex_Impl( const ::rtl::OUString &rPropName ) const
116 {
117     PropertyValue **ppPV;
118     ppPV = (PropertyValue **)
119             bsearch( &rPropName, _aPropVals.GetData(), _aPropVals.Count(),
120                       sizeof( PropertyValue* ),
121                       SbCompare_UString_PropertyValue_Impl );
122     return ppPV ? ( (ppPV-_aPropVals.GetData()) / sizeof(ppPV) ) : USHRT_MAX;
123 }
124 
125 //----------------------------------------------------------------------------
126 
127 void SbPropertyValues::setPropertyValue(
128                     const ::rtl::OUString& aPropertyName,
129                     const Any& aValue)
130                     throw (::com::sun::star::beans::UnknownPropertyException,
131                     ::com::sun::star::beans::PropertyVetoException,
132                     ::com::sun::star::lang::IllegalArgumentException,
133                     ::com::sun::star::lang::WrappedTargetException,
134                     ::com::sun::star::uno::RuntimeException)
135 {
136     sal_Int32 nIndex = GetIndex_Impl( aPropertyName );
137     PropertyValue *pPropVal = _aPropVals.GetObject(
138         sal::static_int_cast< sal_uInt16 >(nIndex));
139     pPropVal->Value = aValue;
140 }
141 
142 //----------------------------------------------------------------------------
143 
144 Any SbPropertyValues::getPropertyValue(
145                     const ::rtl::OUString& aPropertyName)
146                     throw(::com::sun::star::beans::UnknownPropertyException,
147                     ::com::sun::star::lang::WrappedTargetException,
148                     ::com::sun::star::uno::RuntimeException)
149 {
150     sal_Int32 nIndex = GetIndex_Impl( aPropertyName );
151     if ( nIndex != USHRT_MAX )
152         return _aPropVals.GetObject(
153             sal::static_int_cast< sal_uInt16 >(nIndex))->Value;
154     return Any();
155 }
156 
157 //----------------------------------------------------------------------------
158 
159 void SbPropertyValues::addPropertyChangeListener(
160                     const ::rtl::OUString& aPropertyName,
161                     const Reference< XPropertyChangeListener >& )
162                     throw ()
163 {
164     (void)aPropertyName;
165 }
166 
167 //----------------------------------------------------------------------------
168 
169 void SbPropertyValues::removePropertyChangeListener(
170                     const ::rtl::OUString& aPropertyName,
171                     const Reference< XPropertyChangeListener >& )
172                     throw ()
173 {
174     (void)aPropertyName;
175 }
176 
177 //----------------------------------------------------------------------------
178 
179 void SbPropertyValues::addVetoableChangeListener(
180                     const ::rtl::OUString& aPropertyName,
181                     const Reference< XVetoableChangeListener >& )
182                     throw()
183 {
184     (void)aPropertyName;
185 }
186 
187 //----------------------------------------------------------------------------
188 
189 void SbPropertyValues::removeVetoableChangeListener(
190                     const ::rtl::OUString& aPropertyName,
191                     const Reference< XVetoableChangeListener >& )
192                     throw()
193 {
194     (void)aPropertyName;
195 }
196 
197 //----------------------------------------------------------------------------
198 
199 Sequence< PropertyValue > SbPropertyValues::getPropertyValues(void) throw (::com::sun::star::uno::RuntimeException)
200 {
201     Sequence<PropertyValue> aRet( _aPropVals.Count());
202     for ( sal_uInt16 n = 0; n < _aPropVals.Count(); ++n )
203         aRet.getArray()[n] = *_aPropVals.GetObject(n);
204     return aRet;
205 }
206 
207 //----------------------------------------------------------------------------
208 
209 void SbPropertyValues::setPropertyValues(const Sequence< PropertyValue >& rPropertyValues )
210                      throw (::com::sun::star::beans::UnknownPropertyException,
211                      ::com::sun::star::beans::PropertyVetoException,
212                      ::com::sun::star::lang::IllegalArgumentException,
213                      ::com::sun::star::lang::WrappedTargetException,
214                      ::com::sun::star::uno::RuntimeException)
215 {
216     if ( _aPropVals.Count() )
217         throw PropertyExistException();
218 
219     const PropertyValue *pPropVals = rPropertyValues.getConstArray();
220     for ( sal_Int16 n = 0; n < rPropertyValues.getLength(); ++n )
221     {
222         PropertyValue *pPropVal = new PropertyValue(pPropVals[n]);
223         _aPropVals.Insert( pPropVal, n );
224     }
225 }
226 
227 //============================================================================
228 //PropertySetInfoImpl
229 
230 PropertySetInfoImpl::PropertySetInfoImpl()
231 {
232 }
233 
234 sal_Int32 PropertySetInfoImpl::GetIndex_Impl( const ::rtl::OUString &rPropName ) const
235 {
236     Property *pP;
237     pP = (Property*)
238             bsearch( &rPropName, _aProps.getConstArray(), _aProps.getLength(),
239                       sizeof( Property ),
240                       SbCompare_UString_Property_Impl );
241     return pP ? sal::static_int_cast<sal_Int32>( (pP-_aProps.getConstArray()) / sizeof(pP) ) : -1;
242 }
243 
244 Sequence< Property > PropertySetInfoImpl::getProperties(void) throw()
245 {
246     return _aProps;
247 }
248 
249 Property PropertySetInfoImpl::getPropertyByName(const ::rtl::OUString& Name) throw( RuntimeException )
250 {
251     sal_Int32 nIndex = GetIndex_Impl( Name );
252     if( USHRT_MAX != nIndex )
253         return _aProps.getConstArray()[ nIndex ];
254     return Property();
255 }
256 
257 sal_Bool PropertySetInfoImpl::hasPropertyByName(const ::rtl::OUString& Name) throw( RuntimeException )
258 {
259     sal_Int32 nIndex = GetIndex_Impl( Name );
260     return USHRT_MAX != nIndex;
261 }
262 
263 
264 //============================================================================
265 
266 SbPropertySetInfo::SbPropertySetInfo()
267 {
268 }
269 
270 //----------------------------------------------------------------------------
271 
272 SbPropertySetInfo::SbPropertySetInfo( const SbPropertyValueArr_Impl &rPropVals )
273 {
274     aImpl._aProps.realloc( rPropVals.Count() );
275     for ( sal_uInt16 n = 0; n < rPropVals.Count(); ++n )
276     {
277         Property &rProp = aImpl._aProps.getArray()[n];
278         const PropertyValue &rPropVal = *rPropVals.GetObject(n);
279         rProp.Name = rPropVal.Name;
280         rProp.Handle = rPropVal.Handle;
281         rProp.Type = getCppuVoidType();
282         rProp.Attributes = 0;
283     }
284 }
285 
286 //----------------------------------------------------------------------------
287 
288 SbPropertySetInfo::~SbPropertySetInfo()
289 {
290 }
291 
292 //-------------------------------------------------------------------------
293 
294 Sequence< Property > SbPropertySetInfo::getProperties(void) throw( RuntimeException )
295 {
296     return aImpl.getProperties();
297 }
298 
299 Property SbPropertySetInfo::getPropertyByName(const ::rtl::OUString& Name)
300     throw( RuntimeException )
301 {
302     return aImpl.getPropertyByName( Name );
303 }
304 
305 sal_Bool SbPropertySetInfo::hasPropertyByName(const ::rtl::OUString& Name)
306     throw( RuntimeException )
307 {
308     return aImpl.hasPropertyByName( Name );
309 }
310 
311 
312 //----------------------------------------------------------------------------
313 
314 SbPropertyContainer::SbPropertyContainer()
315 {
316 }
317 
318 //----------------------------------------------------------------------------
319 
320 SbPropertyContainer::~SbPropertyContainer()
321 {
322 }
323 
324 //----------------------------------------------------------------------------
325 void SbPropertyContainer::addProperty(const ::rtl::OUString& Name,
326                                       sal_Int16 Attributes,
327                                       const Any& DefaultValue)
328     throw(  PropertyExistException, IllegalTypeException,
329             IllegalArgumentException, RuntimeException )
330 {
331     (void)Name;
332     (void)Attributes;
333     (void)DefaultValue;
334 }
335 
336 //----------------------------------------------------------------------------
337 void SbPropertyContainer::removeProperty(const ::rtl::OUString& Name)
338     throw( UnknownPropertyException, RuntimeException )
339 {
340     (void)Name;
341 }
342 
343 //----------------------------------------------------------------------------
344 // XPropertySetInfo
345 Sequence< Property > SbPropertyContainer::getProperties(void) throw ()
346 {
347     return aImpl.getProperties();
348 }
349 
350 Property SbPropertyContainer::getPropertyByName(const ::rtl::OUString& Name)
351     throw( RuntimeException )
352 {
353     return aImpl.getPropertyByName( Name );
354 }
355 
356 sal_Bool SbPropertyContainer::hasPropertyByName(const ::rtl::OUString& Name)
357     throw( RuntimeException )
358 {
359     return aImpl.hasPropertyByName( Name );
360 }
361 
362 //----------------------------------------------------------------------------
363 
364 Sequence< PropertyValue > SbPropertyContainer::getPropertyValues(void)
365 {
366     return Sequence<PropertyValue>();
367 }
368 
369 //----------------------------------------------------------------------------
370 
371 void SbPropertyContainer::setPropertyValues(const Sequence< PropertyValue >& PropertyValues_)
372 {
373     (void)PropertyValues_;
374 }
375 
376 //----------------------------------------------------------------------------
377 
378 void RTL_Impl_CreatePropertySet( StarBASIC* pBasic, SbxArray& rPar, sal_Bool bWrite )
379 {
380     (void)pBasic;
381     (void)bWrite;
382 
383     // We need at least one parameter
384     // TODO: In this case < 2 is not correct ;-)
385     if ( rPar.Count() < 2 )
386     {
387         StarBASIC::Error( SbERR_BAD_ARGUMENT );
388         return;
389     }
390 
391     // Get class names of struct
392     String aServiceName( RTL_CONSTASCII_USTRINGPARAM("stardiv.uno.beans.PropertySet") );
393 
394 #if 0
395     // Service suchen und instanzieren
396     Reference< XMultiServiceFactory > xServiceManager = getProcessServiceFactory();
397     Reference< XInterface > xInterface;
398     if( xProv.is() )
399         xInterface = xProv->newInstance();
400 #else
401     Reference< XInterface > xInterface = (OWeakObject*) new SbPropertyValues();
402 #endif
403 
404     SbxVariableRef refVar = rPar.Get(0);
405     if( xInterface.is() )
406     {
407         // Set PropertyValues
408         Any aArgAsAny = sbxToUnoValue( rPar.Get(1),
409                 getCppuType( (Sequence<PropertyValue>*)0 ) );
410         Sequence<PropertyValue> *pArg =
411                 (Sequence<PropertyValue>*) aArgAsAny.getValue();
412         Reference< XPropertyAccess > xPropAcc = Reference< XPropertyAccess >::query( xInterface );
413         xPropAcc->setPropertyValues( *pArg );
414 
415         // Build a SbUnoObject and return it
416         Any aAny;
417         aAny <<= xInterface;
418         SbUnoObjectRef xUnoObj = new SbUnoObject( aServiceName, aAny );
419         if( xUnoObj->getUnoAny().getValueType().getTypeClass() != TypeClass_VOID )
420         {
421             // Return object
422             refVar->PutObject( (SbUnoObject*)xUnoObj );
423             return;
424         }
425     }
426 
427     // Object could not be created
428     refVar->PutObject( NULL );
429 }
430 
431