1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "propacc.hxx"
23 #include <basic/sbstar.hxx>
24 #include <sbunoobj.hxx>
25 #include <basic/sbuno.hxx>
27 using com::sun::star::uno::Reference
;
28 using namespace com::sun::star
;
29 using namespace com::sun::star::uno
;
30 using namespace com::sun::star::lang
;
31 using namespace com::sun::star::beans
;
41 int CDECL
SbCompare_PropertyValues_Impl( const void *arg1
, const void *arg2
)
43 return ((PropertyValue
*)arg1
)->Name
.compareTo( ((PropertyValue
*)arg2
)->Name
);
46 struct SbCompare_UString_PropertyValue_Impl
48 bool operator() (const OUString
& lhs
, PropertyValue
const & rhs
)
50 return lhs
.compareTo(rhs
.Name
) < 0;
52 bool operator() (PropertyValue
const & lhs
, const OUString
& rhs
)
54 return lhs
.Name
.compareTo(rhs
) < 0;
57 bool operator() (PropertyValue
const & lhs
, const PropertyValue
& rhs
)
59 return lhs
.Name
.compareTo(rhs
.Name
) < 0;
64 int CDECL
SbCompare_Properties_Impl( const void *arg1
, const void *arg2
)
66 return ((Property
*)arg1
)->Name
.compareTo( ((Property
*)arg2
)->Name
);
69 extern "C" int CDECL
SbCompare_UString_Property_Impl( const void *arg1
, const void *arg2
)
71 const OUString
*pArg1
= (OUString
*) arg1
;
72 const Property
*pArg2
= (Property
*) arg2
;
73 return pArg1
->compareTo( pArg2
->Name
);
76 //----------------------------------------------------------------------------
78 SbPropertyValues::SbPropertyValues()
82 //----------------------------------------------------------------------------
84 SbPropertyValues::~SbPropertyValues()
86 m_xInfo
= Reference
< XPropertySetInfo
>();
89 //----------------------------------------------------------------------------
91 Reference
< XPropertySetInfo
> SbPropertyValues::getPropertySetInfo(void) throw( RuntimeException
)
96 SbPropertySetInfo
*pInfo
= new SbPropertySetInfo( m_aPropVals
);
102 //-------------------------------------------------------------------------
104 size_t SbPropertyValues::GetIndex_Impl( const OUString
&rPropName
) const
106 SbPropertyValueArr_Impl::const_iterator it
= std::lower_bound(
107 m_aPropVals
.begin(), m_aPropVals
.end(), rPropName
,
108 SbCompare_UString_PropertyValue_Impl() );
109 if (it
== m_aPropVals
.end())
111 throw beans::UnknownPropertyException(
112 "Property not found: " + rPropName
,
113 const_cast<SbPropertyValues
&>(*this));
115 return it
- m_aPropVals
.begin();
118 //----------------------------------------------------------------------------
120 void SbPropertyValues::setPropertyValue(
121 const OUString
& aPropertyName
,
123 throw (::com::sun::star::beans::UnknownPropertyException
,
124 ::com::sun::star::beans::PropertyVetoException
,
125 ::com::sun::star::lang::IllegalArgumentException
,
126 ::com::sun::star::lang::WrappedTargetException
,
127 ::com::sun::star::uno::RuntimeException
)
129 size_t const nIndex
= GetIndex_Impl( aPropertyName
);
130 PropertyValue
& rPropVal
= m_aPropVals
[nIndex
];
131 rPropVal
.Value
= aValue
;
134 //----------------------------------------------------------------------------
136 Any
SbPropertyValues::getPropertyValue(
137 const OUString
& aPropertyName
)
138 throw(::com::sun::star::beans::UnknownPropertyException
,
139 ::com::sun::star::lang::WrappedTargetException
,
140 ::com::sun::star::uno::RuntimeException
)
142 size_t const nIndex
= GetIndex_Impl( aPropertyName
);
143 return m_aPropVals
[nIndex
].Value
;
146 //----------------------------------------------------------------------------
148 void SbPropertyValues::addPropertyChangeListener(
149 const OUString
& aPropertyName
,
150 const Reference
< XPropertyChangeListener
>& )
156 //----------------------------------------------------------------------------
158 void SbPropertyValues::removePropertyChangeListener(
159 const OUString
& aPropertyName
,
160 const Reference
< XPropertyChangeListener
>& )
166 //----------------------------------------------------------------------------
168 void SbPropertyValues::addVetoableChangeListener(
169 const OUString
& aPropertyName
,
170 const Reference
< XVetoableChangeListener
>& )
176 //----------------------------------------------------------------------------
178 void SbPropertyValues::removeVetoableChangeListener(
179 const OUString
& aPropertyName
,
180 const Reference
< XVetoableChangeListener
>& )
186 //----------------------------------------------------------------------------
188 Sequence
< PropertyValue
> SbPropertyValues::getPropertyValues(void) throw (::com::sun::star::uno::RuntimeException
)
190 Sequence
<PropertyValue
> aRet( m_aPropVals
.size() );
191 for (size_t n
= 0; n
< m_aPropVals
.size(); ++n
)
192 aRet
.getArray()[n
] = m_aPropVals
[n
];
196 //----------------------------------------------------------------------------
198 void SbPropertyValues::setPropertyValues(const Sequence
< PropertyValue
>& rPropertyValues
)
199 throw (::com::sun::star::beans::UnknownPropertyException
,
200 ::com::sun::star::beans::PropertyVetoException
,
201 ::com::sun::star::lang::IllegalArgumentException
,
202 ::com::sun::star::lang::WrappedTargetException
,
203 ::com::sun::star::uno::RuntimeException
)
205 if ( !m_aPropVals
.empty() )
206 throw PropertyExistException();
208 const PropertyValue
*pPropVals
= rPropertyValues
.getConstArray();
209 for (sal_Int32 n
= 0; n
< rPropertyValues
.getLength(); ++n
)
211 PropertyValue
*pPropVal
= new PropertyValue(pPropVals
[n
]);
212 m_aPropVals
.push_back( pPropVal
);
216 //============================================================================
217 //PropertySetInfoImpl
219 PropertySetInfoImpl::PropertySetInfoImpl()
223 sal_Int32
PropertySetInfoImpl::GetIndex_Impl( const OUString
&rPropName
) const
227 bsearch( &rPropName
, _aProps
.getConstArray(), _aProps
.getLength(),
229 SbCompare_UString_Property_Impl
);
230 return pP
? sal::static_int_cast
<sal_Int32
>( pP
- _aProps
.getConstArray() ) : -1;
233 Sequence
< Property
> PropertySetInfoImpl::getProperties(void) throw()
238 Property
PropertySetInfoImpl::getPropertyByName(const OUString
& Name
) throw( RuntimeException
)
240 sal_Int32 nIndex
= GetIndex_Impl( Name
);
241 if( USHRT_MAX
!= nIndex
)
242 return _aProps
.getConstArray()[ nIndex
];
246 sal_Bool
PropertySetInfoImpl::hasPropertyByName(const OUString
& Name
) throw( RuntimeException
)
248 sal_Int32 nIndex
= GetIndex_Impl( Name
);
249 return USHRT_MAX
!= nIndex
;
253 //----------------------------------------------------------------------------
255 SbPropertySetInfo::SbPropertySetInfo( const SbPropertyValueArr_Impl
&rPropVals
)
257 aImpl
._aProps
.realloc( rPropVals
.size() );
258 for ( sal_uInt16 n
= 0; n
< rPropVals
.size(); ++n
)
260 Property
&rProp
= aImpl
._aProps
.getArray()[n
];
261 const PropertyValue
&rPropVal
= rPropVals
[n
];
262 rProp
.Name
= rPropVal
.Name
;
263 rProp
.Handle
= rPropVal
.Handle
;
264 rProp
.Type
= getCppuVoidType();
265 rProp
.Attributes
= 0;
269 //----------------------------------------------------------------------------
271 SbPropertySetInfo::~SbPropertySetInfo()
275 //-------------------------------------------------------------------------
277 Sequence
< Property
> SbPropertySetInfo::getProperties(void) throw( RuntimeException
)
279 return aImpl
.getProperties();
282 Property
SbPropertySetInfo::getPropertyByName(const OUString
& Name
)
283 throw( RuntimeException
)
285 return aImpl
.getPropertyByName( Name
);
288 sal_Bool
SbPropertySetInfo::hasPropertyByName(const OUString
& Name
)
289 throw( RuntimeException
)
291 return aImpl
.hasPropertyByName( Name
);
294 //----------------------------------------------------------------------------
296 void RTL_Impl_CreatePropertySet( StarBASIC
* pBasic
, SbxArray
& rPar
, sal_Bool bWrite
)
301 // We need at least one parameter
302 // TODO: In this case < 2 is not correct ;-)
303 if ( rPar
.Count() < 2 )
305 StarBASIC::Error( SbERR_BAD_ARGUMENT
);
309 // Get class names of struct
310 OUString
aServiceName( "stardiv.uno.beans.PropertySet");
312 Reference
< XInterface
> xInterface
= (OWeakObject
*) new SbPropertyValues();
314 SbxVariableRef refVar
= rPar
.Get(0);
315 if( xInterface
.is() )
317 // Set PropertyValues
318 Any aArgAsAny
= sbxToUnoValue( rPar
.Get(1),
319 getCppuType( (Sequence
<PropertyValue
>*)0 ) );
320 Sequence
<PropertyValue
> *pArg
=
321 (Sequence
<PropertyValue
>*) aArgAsAny
.getValue();
322 Reference
< XPropertyAccess
> xPropAcc
= Reference
< XPropertyAccess
>::query( xInterface
);
323 xPropAcc
->setPropertyValues( *pArg
);
325 // Build a SbUnoObject and return it
328 SbUnoObjectRef xUnoObj
= new SbUnoObject( aServiceName
, aAny
);
329 if( xUnoObj
->getUnoAny().getValueType().getTypeClass() != TypeClass_VOID
)
332 refVar
->PutObject( (SbUnoObject
*)xUnoObj
);
337 // Object could not be created
338 refVar
->PutObject( NULL
);
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */