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