diff options
author | Andreas Bregas <ab@openoffice.org> | 2010-01-26 11:52:52 +0100 |
---|---|---|
committer | Andreas Bregas <ab@openoffice.org> | 2010-01-26 11:52:52 +0100 |
commit | 10a6f8b96a40912b0e89705570806de3aca41e3e (patch) | |
tree | 436af6b6e806459406c0dc4f691452d32c71c7a8 /basic/source | |
parent | b4083630cb439a9f54340db6840e8bc6cfcc42c8 (diff) |
ab76: #i107070#: Singleton support
Diffstat (limited to 'basic/source')
-rwxr-xr-x[-rw-r--r--] | basic/source/classes/sbunoobj.cxx | 107 | ||||
-rw-r--r-- | basic/source/inc/sbunoobj.hxx | 18 |
2 files changed, 120 insertions, 5 deletions
diff --git a/basic/source/classes/sbunoobj.cxx b/basic/source/classes/sbunoobj.cxx index a6ae8cac29af..08bed1019116 100644..100755 --- a/basic/source/classes/sbunoobj.cxx +++ b/basic/source/classes/sbunoobj.cxx @@ -106,6 +106,7 @@ TYPEINIT1(SbUnoObject,SbxObject) TYPEINIT1(SbUnoClass,SbxObject) TYPEINIT1(SbUnoService,SbxObject) TYPEINIT1(SbUnoServiceCtor,SbxMethod) +TYPEINIT1(SbUnoSingleton,SbxObject) typedef WeakImplHelper1< XAllListener > BasicAllListenerHelper; @@ -750,11 +751,11 @@ void unoToSbxValue( SbxVariable* pVar, const Any& aValue ) // SbUnoObject instanzieren String aName; SbUnoObject* pSbUnoObject = new SbUnoObject( aName, aValue ); - //If this is called externally e.g. from the scripting
- //framework then there is no 'active' runtime the default property will not be set up
- //only a vba object will have XDefaultProp set anyway so... this
- //test seems a bit of overkill
- //if ( SbiRuntime::isVBAEnabled() )
+ //If this is called externally e.g. from the scripting + //framework then there is no 'active' runtime the default property will not be set up + //only a vba object will have XDefaultProp set anyway so... this + //test seems a bit of overkill + //if ( SbiRuntime::isVBAEnabled() ) { String sDfltPropName; @@ -3302,6 +3303,18 @@ SbxVariable* SbUnoClass::Find( const XubString& rName, SbxClassType t ) pRes->PutObject( xWrapper ); } } + + // An UNO singleton? + if( !pRes ) + { + SbUnoSingleton* pUnoSingleton = findUnoSingleton( aNewName ); + if( pUnoSingleton ) + { + pRes = new SbxVariable( SbxVARIANT ); + SbxObjectRef xWrapper = (SbxObject*)pUnoSingleton; + pRes->PutObject( xWrapper ); + } + } } } @@ -3581,6 +3594,90 @@ SbxInfo* SbUnoServiceCtor::GetInfo() } +SbUnoSingleton* findUnoSingleton( const String& rName ) +{ + SbUnoSingleton* pSbUnoSingleton = NULL; + + Reference< XHierarchicalNameAccess > xTypeAccess = getTypeProvider_Impl(); + if( xTypeAccess->hasByHierarchicalName( rName ) ) + { + Any aRet = xTypeAccess->getByHierarchicalName( rName ); + Reference< XTypeDescription > xTypeDesc; + aRet >>= xTypeDesc; + + if( xTypeDesc.is() ) + { + TypeClass eTypeClass = xTypeDesc->getTypeClass(); + if( eTypeClass == TypeClass_SINGLETON ) + { + Reference< XSingletonTypeDescription > xSingletonTypeDesc( xTypeDesc, UNO_QUERY ); + if( xSingletonTypeDesc.is() ) + pSbUnoSingleton = new SbUnoSingleton( rName, xSingletonTypeDesc ); + } + } + } + return pSbUnoSingleton; +} + +SbUnoSingleton::SbUnoSingleton( const String& aName_, + const Reference< XSingletonTypeDescription >& xSingletonTypeDesc ) + : SbxObject( aName_ ) + , m_xSingletonTypeDesc( xSingletonTypeDesc ) +{ + SbxVariableRef xGetMethodRef = + new SbxMethod( String( RTL_CONSTASCII_USTRINGPARAM( "get" ) ), SbxOBJECT ); + QuickInsert( (SbxVariable*)xGetMethodRef ); +} + +void SbUnoSingleton::SFX_NOTIFY( SfxBroadcaster& rBC, const TypeId& rBCType, + const SfxHint& rHint, const TypeId& rHintType ) +{ + const SbxHint* pHint = PTR_CAST(SbxHint,&rHint); + if( pHint ) + { + SbxVariable* pVar = pHint->GetVar(); + SbxArray* pParams = pVar->GetParameters(); + UINT32 nParamCount = pParams ? ((UINT32)pParams->Count() - 1) : 0; + UINT32 nAllowedParamCount = 1; + + Reference < XComponentContext > xContextToUse; + if( nParamCount > 0 ) + { + // Check if first parameter is a context and use it then + Reference < XComponentContext > xFirstParamContext; + Any aArg1 = sbxToUnoValue( pParams->Get( 1 ) ); + if( (aArg1 >>= xFirstParamContext) && xFirstParamContext.is() ) + xContextToUse = xFirstParamContext; + } + + if( !xContextToUse.is() ) + { + Reference < XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), UNO_QUERY_THROW ); + xContextToUse.set( xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" )) ), UNO_QUERY_THROW ); + --nAllowedParamCount; + } + + if( nParamCount > nAllowedParamCount ) + { + StarBASIC::Error( SbERR_BAD_ARGUMENT ); + return; + } + + Any aRetAny; + if( xContextToUse.is() ) + { + String aSingletonName( RTL_CONSTASCII_USTRINGPARAM("/singletons/") ); + aSingletonName += GetName(); + Reference < XInterface > xRet; + xContextToUse->getValueByName( aSingletonName ) >>= xRet; + aRetAny <<= xRet; + } + unoToSbxValue( pVar, aRetAny ); + } + else + SbxObject::SFX_NOTIFY( rBC, rBCType, rHint, rHintType ); +} + //======================================================================== //======================================================================== diff --git a/basic/source/inc/sbunoobj.hxx b/basic/source/inc/sbunoobj.hxx index 6889aa196c54..c7ba2b9f6aaf 100644 --- a/basic/source/inc/sbunoobj.hxx +++ b/basic/source/inc/sbunoobj.hxx @@ -44,6 +44,7 @@ #include <com/sun/star/script/XInvocation.hpp> #include <com/sun/star/reflection/XIdlClass.hpp> #include <com/sun/star/reflection/XServiceTypeDescription2.hpp> +#include <com/sun/star/reflection/XSingletonTypeDescription.hpp> #include <rtl/ustring.hxx> class SbUnoObject: public SbxObject @@ -229,6 +230,23 @@ public: }; +// Wrapper for UNO Singleton +class SbUnoSingleton : public SbxObject +{ + const ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XSingletonTypeDescription > m_xSingletonTypeDesc; + +public: + TYPEINFO(); + SbUnoSingleton( const String& aName_, + const ::com::sun::star::uno::Reference< ::com::sun::star::reflection::XSingletonTypeDescription >& xSingletonTypeDesc ); + + void SFX_NOTIFY( SfxBroadcaster&, const TypeId&, const SfxHint& rHint, const TypeId& ); +}; +SV_DECL_IMPL_REF(SbUnoSingleton); + +SbUnoSingleton* findUnoSingleton( const String& rName ); + + // #105565 Special Object to wrap a strongly typed Uno Any class SbUnoAnyObject: public SbxObject { |