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 56 int CDECL SbCompare_PropertyValues_Impl( const void *arg1, const void *arg2 ) 57 { 58 return ((PropertyValue*)arg1)->Name.compareTo( ((PropertyValue*)arg2)->Name ); 59 } 60 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 68 int CDECL SbCompare_Properties_Impl( const void *arg1, const void *arg2 ) 69 { 70 return ((Property*)arg1)->Name.compareTo( ((Property*)arg2)->Name ); 71 } 72 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 82 SbPropertyValues::SbPropertyValues() 83 { 84 } 85 86 //---------------------------------------------------------------------------- 87 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 98 Reference< XPropertySetInfo > SbPropertyValues::getPropertySetInfo(void) 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 111 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 123 void SbPropertyValues::setPropertyValue( 124 const ::rtl::OUString& aPropertyName, 125 const Any& aValue) 126 { 127 sal_Int32 nIndex = GetIndex_Impl( aPropertyName ); 128 PropertyValue *pPropVal = _aPropVals.GetObject( 129 sal::static_int_cast< sal_uInt16 >(nIndex)); 130 pPropVal->Value = aValue; 131 } 132 133 //---------------------------------------------------------------------------- 134 135 Any SbPropertyValues::getPropertyValue( 136 const ::rtl::OUString& aPropertyName) 137 { 138 sal_Int32 nIndex = GetIndex_Impl( aPropertyName ); 139 if ( nIndex != USHRT_MAX ) 140 return _aPropVals.GetObject( 141 sal::static_int_cast< sal_uInt16 >(nIndex))->Value; 142 return Any(); 143 } 144 145 //---------------------------------------------------------------------------- 146 147 void SbPropertyValues::addPropertyChangeListener( 148 const ::rtl::OUString& aPropertyName, 149 const Reference< XPropertyChangeListener >& ) 150 throw () 151 { 152 (void)aPropertyName; 153 } 154 155 //---------------------------------------------------------------------------- 156 157 void SbPropertyValues::removePropertyChangeListener( 158 const ::rtl::OUString& aPropertyName, 159 const Reference< XPropertyChangeListener >& ) 160 throw () 161 { 162 (void)aPropertyName; 163 } 164 165 //---------------------------------------------------------------------------- 166 167 void SbPropertyValues::addVetoableChangeListener( 168 const ::rtl::OUString& aPropertyName, 169 const Reference< XVetoableChangeListener >& ) 170 throw() 171 { 172 (void)aPropertyName; 173 } 174 175 //---------------------------------------------------------------------------- 176 177 void SbPropertyValues::removeVetoableChangeListener( 178 const ::rtl::OUString& aPropertyName, 179 const Reference< XVetoableChangeListener >& ) 180 throw() 181 { 182 (void)aPropertyName; 183 } 184 185 //---------------------------------------------------------------------------- 186 187 Sequence< PropertyValue > SbPropertyValues::getPropertyValues(void) 188 { 189 Sequence<PropertyValue> aRet( _aPropVals.Count()); 190 for ( sal_uInt16 n = 0; n < _aPropVals.Count(); ++n ) 191 aRet.getArray()[n] = *_aPropVals.GetObject(n); 192 return aRet; 193 } 194 195 //---------------------------------------------------------------------------- 196 197 void SbPropertyValues::setPropertyValues(const Sequence< PropertyValue >& rPropertyValues ) 198 { 199 if ( _aPropVals.Count() ) 200 throw PropertyExistException(); 201 202 const PropertyValue *pPropVals = rPropertyValues.getConstArray(); 203 for ( sal_Int16 n = 0; n < rPropertyValues.getLength(); ++n ) 204 { 205 PropertyValue *pPropVal = new PropertyValue(pPropVals[n]); 206 _aPropVals.Insert( pPropVal, n ); 207 } 208 } 209 210 //============================================================================ 211 //PropertySetInfoImpl 212 213 PropertySetInfoImpl::PropertySetInfoImpl() 214 { 215 } 216 217 sal_Int32 PropertySetInfoImpl::GetIndex_Impl( const ::rtl::OUString &rPropName ) const 218 { 219 Property *pP; 220 pP = (Property*) 221 bsearch( &rPropName, _aProps.getConstArray(), _aProps.getLength(), 222 sizeof( Property ), 223 SbCompare_UString_Property_Impl ); 224 return pP ? sal::static_int_cast<sal_Int32>( (pP-_aProps.getConstArray()) / sizeof(pP) ) : -1; 225 } 226 227 Sequence< Property > PropertySetInfoImpl::getProperties(void) throw() 228 { 229 return _aProps; 230 } 231 232 Property PropertySetInfoImpl::getPropertyByName(const ::rtl::OUString& Name) 233 { 234 sal_Int32 nIndex = GetIndex_Impl( Name ); 235 if( USHRT_MAX != nIndex ) 236 return _aProps.getConstArray()[ nIndex ]; 237 return Property(); 238 } 239 240 sal_Bool PropertySetInfoImpl::hasPropertyByName(const ::rtl::OUString& Name) 241 { 242 sal_Int32 nIndex = GetIndex_Impl( Name ); 243 return USHRT_MAX != nIndex; 244 } 245 246 247 //============================================================================ 248 249 SbPropertySetInfo::SbPropertySetInfo() 250 { 251 } 252 253 //---------------------------------------------------------------------------- 254 255 SbPropertySetInfo::SbPropertySetInfo( const SbPropertyValueArr_Impl &rPropVals ) 256 { 257 aImpl._aProps.realloc( rPropVals.Count() ); 258 for ( sal_uInt16 n = 0; n < rPropVals.Count(); ++n ) 259 { 260 Property &rProp = aImpl._aProps.getArray()[n]; 261 const PropertyValue &rPropVal = *rPropVals.GetObject(n); 262 rProp.Name = rPropVal.Name; 263 rProp.Handle = rPropVal.Handle; 264 rProp.Type = getCppuVoidType(); 265 rProp.Attributes = 0; 266 } 267 } 268 269 //---------------------------------------------------------------------------- 270 271 SbPropertySetInfo::~SbPropertySetInfo() 272 { 273 } 274 275 //------------------------------------------------------------------------- 276 277 Sequence< Property > SbPropertySetInfo::getProperties(void) 278 { 279 return aImpl.getProperties(); 280 } 281 282 Property SbPropertySetInfo::getPropertyByName(const ::rtl::OUString& Name) 283 { 284 return aImpl.getPropertyByName( Name ); 285 } 286 287 sal_Bool SbPropertySetInfo::hasPropertyByName(const ::rtl::OUString& Name) 288 { 289 return aImpl.hasPropertyByName( Name ); 290 } 291 292 293 //---------------------------------------------------------------------------- 294 295 SbPropertyContainer::SbPropertyContainer() 296 { 297 } 298 299 //---------------------------------------------------------------------------- 300 301 SbPropertyContainer::~SbPropertyContainer() 302 { 303 } 304 305 //---------------------------------------------------------------------------- 306 void SbPropertyContainer::addProperty(const ::rtl::OUString& Name, 307 sal_Int16 Attributes, 308 const Any& DefaultValue) 309 { 310 (void)Name; 311 (void)Attributes; 312 (void)DefaultValue; 313 } 314 315 //---------------------------------------------------------------------------- 316 void SbPropertyContainer::removeProperty(const ::rtl::OUString& Name) 317 { 318 (void)Name; 319 } 320 321 //---------------------------------------------------------------------------- 322 // XPropertySetInfo 323 Sequence< Property > SbPropertyContainer::getProperties(void) throw () 324 { 325 return aImpl.getProperties(); 326 } 327 328 Property SbPropertyContainer::getPropertyByName(const ::rtl::OUString& Name) 329 { 330 return aImpl.getPropertyByName( Name ); 331 } 332 333 sal_Bool SbPropertyContainer::hasPropertyByName(const ::rtl::OUString& Name) 334 { 335 return aImpl.hasPropertyByName( Name ); 336 } 337 338 //---------------------------------------------------------------------------- 339 340 Sequence< PropertyValue > SbPropertyContainer::getPropertyValues(void) 341 { 342 return Sequence<PropertyValue>(); 343 } 344 345 //---------------------------------------------------------------------------- 346 347 void SbPropertyContainer::setPropertyValues(const Sequence< PropertyValue >& PropertyValues_) 348 { 349 (void)PropertyValues_; 350 } 351 352 //---------------------------------------------------------------------------- 353 354 void RTL_Impl_CreatePropertySet( StarBASIC* pBasic, SbxArray& rPar, sal_Bool bWrite ) 355 { 356 (void)pBasic; 357 (void)bWrite; 358 359 // We need at least one parameter 360 // TODO: In this case < 2 is not correct ;-) 361 if ( rPar.Count() < 2 ) 362 { 363 StarBASIC::Error( SbERR_BAD_ARGUMENT ); 364 return; 365 } 366 367 // Get class names of struct 368 String aServiceName( RTL_CONSTASCII_USTRINGPARAM("stardiv.uno.beans.PropertySet") ); 369 370 #if 0 371 // Service suchen und instanzieren 372 Reference< XMultiServiceFactory > xServiceManager = getProcessServiceFactory(); 373 Reference< XInterface > xInterface; 374 if( xProv.is() ) 375 xInterface = xProv->newInstance(); 376 #else 377 Reference< XInterface > xInterface = (OWeakObject*) new SbPropertyValues(); 378 #endif 379 380 SbxVariableRef refVar = rPar.Get(0); 381 if( xInterface.is() ) 382 { 383 // Set PropertyValues 384 Any aArgAsAny = sbxToUnoValue( rPar.Get(1), 385 getCppuType( (Sequence<PropertyValue>*)0 ) ); 386 Sequence<PropertyValue> *pArg = 387 (Sequence<PropertyValue>*) aArgAsAny.getValue(); 388 Reference< XPropertyAccess > xPropAcc = Reference< XPropertyAccess >::query( xInterface ); 389 xPropAcc->setPropertyValues( *pArg ); 390 391 // Build a SbUnoObject and return it 392 Any aAny; 393 aAny <<= xInterface; 394 SbUnoObjectRef xUnoObj = new SbUnoObject( aServiceName, aAny ); 395 if( xUnoObj->getUnoAny().getValueType().getTypeClass() != TypeClass_VOID ) 396 { 397 // Return object 398 refVar->PutObject( (SbUnoObject*)xUnoObj ); 399 return; 400 } 401 } 402 403 // Object could not be created 404 refVar->PutObject( NULL ); 405 } 406