1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 30*cdf0e10cSrcweir #include "comphelper/propertystatecontainer.hxx" 31*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir //......................................................................... 34*cdf0e10cSrcweir namespace comphelper 35*cdf0e10cSrcweir { 36*cdf0e10cSrcweir //......................................................................... 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 39*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 40*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir namespace 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir static ::rtl::OUString lcl_getUnknownPropertyErrorMessage( const ::rtl::OUString& _rPropertyName ) 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir // TODO: perhaps it's time to think about resources in the comphelper module? 47*cdf0e10cSrcweir // Would be nice to have localized exception strings (a simply resource file containing 48*cdf0e10cSrcweir // strings only would suffice, and could be realized with an UNO service, so we do not 49*cdf0e10cSrcweir // need the dependency to the Tools project) 50*cdf0e10cSrcweir ::rtl::OUStringBuffer sMessage; 51*cdf0e10cSrcweir sMessage.appendAscii( "The property \"" ); 52*cdf0e10cSrcweir sMessage.append( _rPropertyName ); 53*cdf0e10cSrcweir sMessage.appendAscii( "\" is unknown." ); 54*cdf0e10cSrcweir return sMessage.makeStringAndClear(); 55*cdf0e10cSrcweir } 56*cdf0e10cSrcweir } 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir //===================================================================== 59*cdf0e10cSrcweir //= OPropertyStateContainer 60*cdf0e10cSrcweir //===================================================================== 61*cdf0e10cSrcweir //--------------------------------------------------------------------- 62*cdf0e10cSrcweir OPropertyStateContainer::OPropertyStateContainer( ::cppu::OBroadcastHelper& _rBHelper ) 63*cdf0e10cSrcweir :OPropertyContainer( _rBHelper ) 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir //-------------------------------------------------------------------- 68*cdf0e10cSrcweir Any SAL_CALL OPropertyStateContainer::queryInterface( const Type& _rType ) throw (RuntimeException) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir Any aReturn = OPropertyContainer::queryInterface( _rType ); 71*cdf0e10cSrcweir if ( !aReturn.hasValue() ) 72*cdf0e10cSrcweir aReturn = OPropertyStateContainer_TBase::queryInterface( _rType ); 73*cdf0e10cSrcweir return aReturn; 74*cdf0e10cSrcweir } 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir //-------------------------------------------------------------------- 77*cdf0e10cSrcweir IMPLEMENT_FORWARD_XTYPEPROVIDER2( OPropertyStateContainer, OPropertyContainer, OPropertyStateContainer_TBase ) 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir //-------------------------------------------------------------------- 80*cdf0e10cSrcweir sal_Int32 OPropertyStateContainer::getHandleForName( const ::rtl::OUString& _rPropertyName ) SAL_THROW( ( UnknownPropertyException ) ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir // look up the handle for the name 83*cdf0e10cSrcweir ::cppu::IPropertyArrayHelper& rPH = getInfoHelper(); 84*cdf0e10cSrcweir sal_Int32 nHandle = rPH.getHandleByName( _rPropertyName ); 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir if ( -1 == nHandle ) 87*cdf0e10cSrcweir throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( _rPropertyName ), static_cast< XPropertyState* >( this ) ); 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir return nHandle; 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir //-------------------------------------------------------------------- 93*cdf0e10cSrcweir PropertyState SAL_CALL OPropertyStateContainer::getPropertyState( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException) 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir return getPropertyStateByHandle( getHandleForName( _rPropertyName ) ); 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir //-------------------------------------------------------------------- 99*cdf0e10cSrcweir Sequence< PropertyState > SAL_CALL OPropertyStateContainer::getPropertyStates( const Sequence< ::rtl::OUString >& _rPropertyNames ) throw (UnknownPropertyException, RuntimeException) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir sal_Int32 nProperties = _rPropertyNames.getLength(); 102*cdf0e10cSrcweir Sequence< PropertyState> aStates( nProperties ); 103*cdf0e10cSrcweir if ( !nProperties ) 104*cdf0e10cSrcweir return aStates; 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir #ifdef _DEBUG 107*cdf0e10cSrcweir // precondition: property sequence is sorted (the algorythm below relies on this) 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir const ::rtl::OUString* pNames = _rPropertyNames.getConstArray(); 110*cdf0e10cSrcweir const ::rtl::OUString* pNamesCompare = pNames + 1; 111*cdf0e10cSrcweir const ::rtl::OUString* pNamesEnd = _rPropertyNames.getConstArray() + _rPropertyNames.getLength(); 112*cdf0e10cSrcweir for ( ; pNamesCompare != pNamesEnd; ++pNames, ++pNamesCompare ) 113*cdf0e10cSrcweir OSL_PRECOND( pNames->compareTo( *pNamesCompare ) < 0, 114*cdf0e10cSrcweir "OPropertyStateContainer::getPropertyStates: property sequence not sorted!" ); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir #endif 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir const ::rtl::OUString* pLookup = _rPropertyNames.getConstArray(); 119*cdf0e10cSrcweir const ::rtl::OUString* pLookupEnd = pLookup + nProperties; 120*cdf0e10cSrcweir PropertyState* pStates = aStates.getArray(); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir cppu::IPropertyArrayHelper& rHelper = getInfoHelper(); 123*cdf0e10cSrcweir Sequence< Property> aAllProperties = rHelper.getProperties(); 124*cdf0e10cSrcweir sal_Int32 nAllProperties = aAllProperties.getLength(); 125*cdf0e10cSrcweir const Property* pAllProperties = aAllProperties.getConstArray(); 126*cdf0e10cSrcweir const Property* pAllPropertiesEnd = pAllProperties + nAllProperties; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir osl::MutexGuard aGuard( rBHelper.rMutex ); 129*cdf0e10cSrcweir for ( ; ( pAllProperties != pAllPropertiesEnd ) && ( pLookup != pLookupEnd ); ++pAllProperties ) 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir #ifdef _DEBUG 132*cdf0e10cSrcweir if ( pAllProperties < pAllPropertiesEnd - 1 ) 133*cdf0e10cSrcweir OSL_ENSURE( pAllProperties->Name.compareTo( (pAllProperties + 1)->Name ) < 0, 134*cdf0e10cSrcweir "OPropertyStateContainer::getPropertyStates: all-properties not sorted!" ); 135*cdf0e10cSrcweir #endif 136*cdf0e10cSrcweir if ( pAllProperties->Name.equals( *pLookup ) ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir *pStates++ = getPropertyState( *pLookup ); 139*cdf0e10cSrcweir ++pLookup; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir if ( pLookup != pLookupEnd ) 144*cdf0e10cSrcweir // we run out of properties from the IPropertyArrayHelper, but still have properties to lookup 145*cdf0e10cSrcweir // -> we were asked for a nonexistent property 146*cdf0e10cSrcweir throw UnknownPropertyException( lcl_getUnknownPropertyErrorMessage( *pLookup ), static_cast< XPropertyState* >( this ) ); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir return aStates; 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir //-------------------------------------------------------------------- 152*cdf0e10cSrcweir void SAL_CALL OPropertyStateContainer::setPropertyToDefault( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir setPropertyToDefaultByHandle( getHandleForName( _rPropertyName ) ); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir //-------------------------------------------------------------------- 158*cdf0e10cSrcweir Any SAL_CALL OPropertyStateContainer::getPropertyDefault( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir Any aDefault; 161*cdf0e10cSrcweir getPropertyDefaultByHandle( getHandleForName( _rPropertyName ), aDefault ); 162*cdf0e10cSrcweir return aDefault; 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir //-------------------------------------------------------------------- 166*cdf0e10cSrcweir PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir // simply compare the current and the default value 169*cdf0e10cSrcweir Any aCurrentValue; getFastPropertyValue( aCurrentValue, _nHandle ); 170*cdf0e10cSrcweir Any aDefaultValue; getPropertyDefaultByHandle( _nHandle, aDefaultValue ); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir sal_Bool bEqual = uno_type_equalData( 173*cdf0e10cSrcweir const_cast< void* >( aCurrentValue.getValue() ), aCurrentValue.getValueType().getTypeLibType(), 174*cdf0e10cSrcweir const_cast< void* >( aDefaultValue.getValue() ), aDefaultValue.getValueType().getTypeLibType(), 175*cdf0e10cSrcweir reinterpret_cast< uno_QueryInterfaceFunc >(cpp_queryInterface), 176*cdf0e10cSrcweir reinterpret_cast< uno_ReleaseFunc >(cpp_release) 177*cdf0e10cSrcweir ); 178*cdf0e10cSrcweir if ( bEqual ) 179*cdf0e10cSrcweir return PropertyState_DEFAULT_VALUE; 180*cdf0e10cSrcweir else 181*cdf0e10cSrcweir return PropertyState_DIRECT_VALUE; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir //-------------------------------------------------------------------- 185*cdf0e10cSrcweir void OPropertyStateContainer::setPropertyToDefaultByHandle( sal_Int32 _nHandle ) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir Any aDefault; 188*cdf0e10cSrcweir getPropertyDefaultByHandle( _nHandle, aDefault ); 189*cdf0e10cSrcweir setFastPropertyValue( _nHandle, aDefault ); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir //......................................................................... 193*cdf0e10cSrcweir } // namespace comphelper 194*cdf0e10cSrcweir //......................................................................... 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir #ifdef FS_PRIV_DEBUG 197*cdf0e10cSrcweir #define STATECONTAINER_TEST 198*cdf0e10cSrcweir #endif 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir #ifdef STATECONTAINER_TEST 201*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp> 202*cdf0e10cSrcweir #include <comphelper/proparrhlp.hxx> 203*cdf0e10cSrcweir #include <comphelper/broadcasthelper.hxx> 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir //......................................................................... 206*cdf0e10cSrcweir namespace comphelper 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir //......................................................................... 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 211*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir //===================================================================== 214*cdf0e10cSrcweir //= Test - compiler test 215*cdf0e10cSrcweir //===================================================================== 216*cdf0e10cSrcweir typedef ::cppu::OWeakAggObject Test_RefCountBase; 217*cdf0e10cSrcweir class Test :public OMutexAndBroadcastHelper 218*cdf0e10cSrcweir ,public OPropertyStateContainer 219*cdf0e10cSrcweir ,public OPropertyArrayUsageHelper< Test > 220*cdf0e10cSrcweir ,public Test_RefCountBase 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir private: 223*cdf0e10cSrcweir ::rtl::OUString m_sStringProperty; 224*cdf0e10cSrcweir Reference< XInterface > m_xInterfaceProperty; 225*cdf0e10cSrcweir Any m_aMayBeVoidProperty; 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir protected: 228*cdf0e10cSrcweir Test( ); 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir DECLARE_XINTERFACE( ) 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir public: 233*cdf0e10cSrcweir static Test* Create( ); 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir protected: 236*cdf0e10cSrcweir virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo( ) throw(RuntimeException); 237*cdf0e10cSrcweir virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper(); 238*cdf0e10cSrcweir virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const; 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir protected: 241*cdf0e10cSrcweir // OPropertyStateContainer overridables 242*cdf0e10cSrcweir virtual void getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _rDefault ) const; 243*cdf0e10cSrcweir }; 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir //--------------------------------------------------------------------- 246*cdf0e10cSrcweir Test::Test( ) 247*cdf0e10cSrcweir :OPropertyStateContainer( GetBroadcastHelper() ) 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir registerProperty( 250*cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "StringProperty" ), 251*cdf0e10cSrcweir 1, 252*cdf0e10cSrcweir PropertyAttribute::BOUND, 253*cdf0e10cSrcweir &m_sStringProperty, 254*cdf0e10cSrcweir ::getCppuType( &m_sStringProperty ) 255*cdf0e10cSrcweir ); 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir registerProperty( 258*cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "InterfaceProperty" ), 259*cdf0e10cSrcweir 2, 260*cdf0e10cSrcweir PropertyAttribute::BOUND, 261*cdf0e10cSrcweir &m_xInterfaceProperty, 262*cdf0e10cSrcweir ::getCppuType( &m_xInterfaceProperty ) 263*cdf0e10cSrcweir ); 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir registerMayBeVoidProperty( 266*cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "IntProperty" ), 267*cdf0e10cSrcweir 3, 268*cdf0e10cSrcweir PropertyAttribute::BOUND, 269*cdf0e10cSrcweir &m_aMayBeVoidProperty, 270*cdf0e10cSrcweir ::getCppuType( static_cast< sal_Int32* >( NULL ) ) 271*cdf0e10cSrcweir ); 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir registerPropertyNoMember( 274*cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "OtherInterfaceProperty" ), 275*cdf0e10cSrcweir 4, 276*cdf0e10cSrcweir PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID, 277*cdf0e10cSrcweir ::getCppuType( static_cast< Reference< XInterface >* >( NULL ) ), 278*cdf0e10cSrcweir NULL 279*cdf0e10cSrcweir ); 280*cdf0e10cSrcweir } 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir //--------------------------------------------------------------------- 283*cdf0e10cSrcweir IMPLEMENT_FORWARD_XINTERFACE2( Test, Test_RefCountBase, OPropertyStateContainer ) 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir //--------------------------------------------------------------------- 286*cdf0e10cSrcweir void Test::getPropertyDefaultByHandle( sal_Int32 _nHandle, Any& _rDefault ) const 287*cdf0e10cSrcweir { 288*cdf0e10cSrcweir switch ( _nHandle ) 289*cdf0e10cSrcweir { 290*cdf0e10cSrcweir case 1: 291*cdf0e10cSrcweir _rDefault = makeAny( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StringPropertyDefault" ) ) ); 292*cdf0e10cSrcweir break; 293*cdf0e10cSrcweir case 2: 294*cdf0e10cSrcweir _rDefault = makeAny( Reference< XInterface >( ) ); 295*cdf0e10cSrcweir break; 296*cdf0e10cSrcweir case 3: 297*cdf0e10cSrcweir // void 298*cdf0e10cSrcweir break; 299*cdf0e10cSrcweir case 4: 300*cdf0e10cSrcweir _rDefault = makeAny( Reference< XInterface >( ) ); 301*cdf0e10cSrcweir break; 302*cdf0e10cSrcweir default: 303*cdf0e10cSrcweir OSL_ENSURE( sal_False, "Test::getPropertyDefaultByHandle: invalid handle!" ); 304*cdf0e10cSrcweir } 305*cdf0e10cSrcweir } 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir //--------------------------------------------------------------------- 308*cdf0e10cSrcweir Reference< XPropertySetInfo > SAL_CALL Test::getPropertySetInfo( ) throw(RuntimeException) 309*cdf0e10cSrcweir { 310*cdf0e10cSrcweir return createPropertySetInfo( getInfoHelper() ); 311*cdf0e10cSrcweir } 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir //--------------------------------------------------------------------- 314*cdf0e10cSrcweir ::cppu::IPropertyArrayHelper& SAL_CALL Test::getInfoHelper() 315*cdf0e10cSrcweir { 316*cdf0e10cSrcweir return *getArrayHelper(); 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir 319*cdf0e10cSrcweir //--------------------------------------------------------------------- 320*cdf0e10cSrcweir ::cppu::IPropertyArrayHelper* Test::createArrayHelper( ) const 321*cdf0e10cSrcweir { 322*cdf0e10cSrcweir Sequence< Property > aProps; 323*cdf0e10cSrcweir describeProperties( aProps ); 324*cdf0e10cSrcweir return new ::cppu::OPropertyArrayHelper( aProps ); 325*cdf0e10cSrcweir } 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir //--------------------------------------------------------------------- 328*cdf0e10cSrcweir Test* Test::Create( ) 329*cdf0e10cSrcweir { 330*cdf0e10cSrcweir Test* pInstance = new Test; 331*cdf0e10cSrcweir return pInstance; 332*cdf0e10cSrcweir } 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir //......................................................................... 335*cdf0e10cSrcweir } // namespace comphelper 336*cdf0e10cSrcweir //......................................................................... 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir #endif 339*cdf0e10cSrcweir 340