1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: propacc.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_basic.hxx"
34 #include "propacc.hxx"
36 #include <tools/urlobj.hxx>
37 #include <tools/errcode.hxx>
38 #include <svtools/svarray.hxx>
39 #include <basic/sbstar.hxx>
40 #include <sbunoobj.hxx>
42 using com::sun::star::uno::Reference
;
43 using namespace com::sun::star::uno
;
44 using namespace com::sun::star::lang
;
45 using namespace com::sun::star::beans
;
49 //========================================================================
51 // Declaration conversion from Sbx to UNO with known target type
52 Any
sbxToUnoValue( SbxVariable
* pVar
, const Type
& rType
, Property
* pUnoProperty
= NULL
);
54 //========================================================================
59 #if defined(UNX) || defined(OS2)
63 int CDECL
SbCompare_PropertyValues_Impl( const void *arg1
, const void *arg2
)
65 return ((PropertyValue
*)arg1
)->Name
.compareTo( ((PropertyValue
*)arg2
)->Name
);
68 extern "C" int CDECL
SbCompare_UString_PropertyValue_Impl( const void *arg1
, const void *arg2
)
70 const ::rtl::OUString
*pArg1
= (::rtl::OUString
*) arg1
;
71 const PropertyValue
**pArg2
= (const PropertyValue
**) arg2
;
72 return pArg1
->compareTo( (*pArg2
)->Name
);
75 int CDECL
SbCompare_Properties_Impl( const void *arg1
, const void *arg2
)
77 return ((Property
*)arg1
)->Name
.compareTo( ((Property
*)arg2
)->Name
);
80 extern "C" int CDECL
SbCompare_UString_Property_Impl( const void *arg1
, const void *arg2
)
82 const ::rtl::OUString
*pArg1
= (::rtl::OUString
*) arg1
;
83 const Property
*pArg2
= (Property
*) arg2
;
84 return pArg1
->compareTo( pArg2
->Name
);
87 //----------------------------------------------------------------------------
89 SbPropertyValues::SbPropertyValues()
93 //----------------------------------------------------------------------------
95 SbPropertyValues::~SbPropertyValues()
97 _xInfo
= Reference
< XPropertySetInfo
>();
99 for ( USHORT n
= 0; n
< _aPropVals
.Count(); ++n
)
100 delete _aPropVals
.GetObject( n
);
103 //----------------------------------------------------------------------------
105 Reference
< XPropertySetInfo
> SbPropertyValues::getPropertySetInfo(void) throw( RuntimeException
)
110 SbPropertySetInfo
*pInfo
= new SbPropertySetInfo( _aPropVals
);
111 ((SbPropertyValues
*)this)->_xInfo
= (XPropertySetInfo
*)pInfo
;
116 //-------------------------------------------------------------------------
118 INT32
SbPropertyValues::GetIndex_Impl( const ::rtl::OUString
&rPropName
) const
120 PropertyValue
**ppPV
;
121 ppPV
= (PropertyValue
**)
122 bsearch( &rPropName
, _aPropVals
.GetData(), _aPropVals
.Count(),
123 sizeof( PropertyValue
* ),
124 SbCompare_UString_PropertyValue_Impl
);
125 return ppPV
? ( (ppPV
-_aPropVals
.GetData()) / sizeof(ppPV
) ) : USHRT_MAX
;
128 //----------------------------------------------------------------------------
130 void SbPropertyValues::setPropertyValue(
131 const ::rtl::OUString
& aPropertyName
,
133 throw (::com::sun::star::beans::UnknownPropertyException
,
134 ::com::sun::star::beans::PropertyVetoException
,
135 ::com::sun::star::lang::IllegalArgumentException
,
136 ::com::sun::star::lang::WrappedTargetException
,
137 ::com::sun::star::uno::RuntimeException
)
139 INT32 nIndex
= GetIndex_Impl( aPropertyName
);
140 PropertyValue
*pPropVal
= _aPropVals
.GetObject(
141 sal::static_int_cast
< USHORT
>(nIndex
));
142 pPropVal
->Value
= aValue
;
145 //----------------------------------------------------------------------------
147 Any
SbPropertyValues::getPropertyValue(
148 const ::rtl::OUString
& aPropertyName
)
149 throw(::com::sun::star::beans::UnknownPropertyException
,
150 ::com::sun::star::lang::WrappedTargetException
,
151 ::com::sun::star::uno::RuntimeException
)
153 INT32 nIndex
= GetIndex_Impl( aPropertyName
);
154 if ( nIndex
!= USHRT_MAX
)
155 return _aPropVals
.GetObject(
156 sal::static_int_cast
< USHORT
>(nIndex
))->Value
;
160 //----------------------------------------------------------------------------
162 void SbPropertyValues::addPropertyChangeListener(
163 const ::rtl::OUString
& aPropertyName
,
164 const Reference
< XPropertyChangeListener
>& )
170 //----------------------------------------------------------------------------
172 void SbPropertyValues::removePropertyChangeListener(
173 const ::rtl::OUString
& aPropertyName
,
174 const Reference
< XPropertyChangeListener
>& )
180 //----------------------------------------------------------------------------
182 void SbPropertyValues::addVetoableChangeListener(
183 const ::rtl::OUString
& aPropertyName
,
184 const Reference
< XVetoableChangeListener
>& )
190 //----------------------------------------------------------------------------
192 void SbPropertyValues::removeVetoableChangeListener(
193 const ::rtl::OUString
& aPropertyName
,
194 const Reference
< XVetoableChangeListener
>& )
200 //----------------------------------------------------------------------------
202 Sequence
< PropertyValue
> SbPropertyValues::getPropertyValues(void) throw (::com::sun::star::uno::RuntimeException
)
204 Sequence
<PropertyValue
> aRet( _aPropVals
.Count());
205 for ( USHORT n
= 0; n
< _aPropVals
.Count(); ++n
)
206 aRet
.getArray()[n
] = *_aPropVals
.GetObject(n
);
210 //----------------------------------------------------------------------------
212 void SbPropertyValues::setPropertyValues(const Sequence
< PropertyValue
>& rPropertyValues
)
213 throw (::com::sun::star::beans::UnknownPropertyException
,
214 ::com::sun::star::beans::PropertyVetoException
,
215 ::com::sun::star::lang::IllegalArgumentException
,
216 ::com::sun::star::lang::WrappedTargetException
,
217 ::com::sun::star::uno::RuntimeException
)
219 if ( _aPropVals
.Count() )
220 throw PropertyExistException();
222 const PropertyValue
*pPropVals
= rPropertyValues
.getConstArray();
223 for ( sal_Int16 n
= 0; n
< rPropertyValues
.getLength(); ++n
)
225 PropertyValue
*pPropVal
= new PropertyValue(pPropVals
[n
]);
226 _aPropVals
.Insert( pPropVal
, n
);
230 //============================================================================
231 //PropertySetInfoImpl
233 PropertySetInfoImpl::PropertySetInfoImpl()
237 INT32
PropertySetInfoImpl::GetIndex_Impl( const ::rtl::OUString
&rPropName
) const
241 bsearch( &rPropName
, _aProps
.getConstArray(), _aProps
.getLength(),
243 SbCompare_UString_Property_Impl
);
244 return pP
? sal::static_int_cast
<INT32
>( (pP
-_aProps
.getConstArray()) / sizeof(pP
) ) : -1;
247 Sequence
< Property
> PropertySetInfoImpl::getProperties(void) throw()
252 Property
PropertySetInfoImpl::getPropertyByName(const ::rtl::OUString
& Name
) throw( RuntimeException
)
254 sal_Int32 nIndex
= GetIndex_Impl( Name
);
255 if( USHRT_MAX
!= nIndex
)
256 return _aProps
.getConstArray()[ nIndex
];
260 sal_Bool
PropertySetInfoImpl::hasPropertyByName(const ::rtl::OUString
& Name
) throw( RuntimeException
)
262 sal_Int32 nIndex
= GetIndex_Impl( Name
);
263 return USHRT_MAX
!= nIndex
;
267 //============================================================================
269 SbPropertySetInfo::SbPropertySetInfo()
273 //----------------------------------------------------------------------------
275 SbPropertySetInfo::SbPropertySetInfo( const SbPropertyValueArr_Impl
&rPropVals
)
277 aImpl
._aProps
.realloc( rPropVals
.Count() );
278 for ( USHORT n
= 0; n
< rPropVals
.Count(); ++n
)
280 Property
&rProp
= aImpl
._aProps
.getArray()[n
];
281 const PropertyValue
&rPropVal
= *rPropVals
.GetObject(n
);
282 rProp
.Name
= rPropVal
.Name
;
283 rProp
.Handle
= rPropVal
.Handle
;
284 rProp
.Type
= getCppuVoidType();
285 rProp
.Attributes
= 0;
289 //----------------------------------------------------------------------------
291 SbPropertySetInfo::~SbPropertySetInfo()
295 //-------------------------------------------------------------------------
297 Sequence
< Property
> SbPropertySetInfo::getProperties(void) throw( RuntimeException
)
299 return aImpl
.getProperties();
302 Property
SbPropertySetInfo::getPropertyByName(const ::rtl::OUString
& Name
)
303 throw( RuntimeException
)
305 return aImpl
.getPropertyByName( Name
);
308 BOOL
SbPropertySetInfo::hasPropertyByName(const ::rtl::OUString
& Name
)
309 throw( RuntimeException
)
311 return aImpl
.hasPropertyByName( Name
);
315 //----------------------------------------------------------------------------
317 SbPropertyContainer::SbPropertyContainer()
321 //----------------------------------------------------------------------------
323 SbPropertyContainer::~SbPropertyContainer()
327 //----------------------------------------------------------------------------
328 void SbPropertyContainer::addProperty(const ::rtl::OUString
& Name
,
330 const Any
& DefaultValue
)
331 throw( PropertyExistException
, IllegalTypeException
,
332 IllegalArgumentException
, RuntimeException
)
339 //----------------------------------------------------------------------------
340 void SbPropertyContainer::removeProperty(const ::rtl::OUString
& Name
)
341 throw( UnknownPropertyException
, RuntimeException
)
346 //----------------------------------------------------------------------------
348 Sequence
< Property
> SbPropertyContainer::getProperties(void) throw ()
350 return aImpl
.getProperties();
353 Property
SbPropertyContainer::getPropertyByName(const ::rtl::OUString
& Name
)
354 throw( RuntimeException
)
356 return aImpl
.getPropertyByName( Name
);
359 BOOL
SbPropertyContainer::hasPropertyByName(const ::rtl::OUString
& Name
)
360 throw( RuntimeException
)
362 return aImpl
.hasPropertyByName( Name
);
365 //----------------------------------------------------------------------------
367 Sequence
< PropertyValue
> SbPropertyContainer::getPropertyValues(void)
369 return Sequence
<PropertyValue
>();
372 //----------------------------------------------------------------------------
374 void SbPropertyContainer::setPropertyValues(const Sequence
< PropertyValue
>& PropertyValues_
)
376 (void)PropertyValues_
;
379 //----------------------------------------------------------------------------
381 void RTL_Impl_CreatePropertySet( StarBASIC
* pBasic
, SbxArray
& rPar
, BOOL bWrite
)
386 // We need at least one parameter
387 // TODO: In this case < 2 is not correct ;-)
388 if ( rPar
.Count() < 2 )
390 StarBASIC::Error( SbERR_BAD_ARGUMENT
);
394 // Get class names of struct
395 String
aServiceName( RTL_CONSTASCII_USTRINGPARAM("stardiv.uno.beans.PropertySet") );
398 // Service suchen und instanzieren
399 Reference
< XMultiServiceFactory
> xServiceManager
= getProcessServiceFactory();
400 Reference
< XInterface
> xInterface
;
402 xInterface
= xProv
->newInstance();
404 Reference
< XInterface
> xInterface
= (OWeakObject
*) new SbPropertyValues();
407 SbxVariableRef refVar
= rPar
.Get(0);
408 if( xInterface
.is() )
410 // Set PropertyValues
411 Any aArgAsAny
= sbxToUnoValue( rPar
.Get(1),
412 getCppuType( (Sequence
<PropertyValue
>*)0 ) );
413 Sequence
<PropertyValue
> *pArg
=
414 (Sequence
<PropertyValue
>*) aArgAsAny
.getValue();
415 Reference
< XPropertyAccess
> xPropAcc
= Reference
< XPropertyAccess
>::query( xInterface
);
416 xPropAcc
->setPropertyValues( *pArg
);
418 // Build a SbUnoObject and return it
421 SbUnoObjectRef xUnoObj
= new SbUnoObject( aServiceName
, aAny
);
422 if( xUnoObj
->getUnoAny().getValueType().getTypeClass() != TypeClass_VOID
)
425 refVar
->PutObject( (SbUnoObject
*)xUnoObj
);
430 // Object could not be created
431 refVar
->PutObject( NULL
);