xref: /trunk/main/eventattacher/source/eventattacher.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
12694e834SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32694e834SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42694e834SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52694e834SAndrew Rist  * distributed with this work for additional information
62694e834SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72694e834SAndrew Rist  * to you under the Apache License, Version 2.0 (the
82694e834SAndrew Rist  * "License"); you may not use this file except in compliance
92694e834SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
112694e834SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
132694e834SAndrew Rist  * Unless required by applicable law or agreed to in writing,
142694e834SAndrew Rist  * software distributed under the License is distributed on an
152694e834SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162694e834SAndrew Rist  * KIND, either express or implied.  See the License for the
172694e834SAndrew Rist  * specific language governing permissions and limitations
182694e834SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
202694e834SAndrew Rist  *************************************************************/
212694e834SAndrew Rist 
222694e834SAndrew Rist 
23cdf0e10cSrcweir #include <osl/diagnose.h>
24cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
25cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
26cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/XIntrospection.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/MethodConcept.hpp>
31cdf0e10cSrcweir #include <com/sun/star/script/XEventAttacher.hpp>
32cdf0e10cSrcweir #include <com/sun/star/script/XTypeConverter.hpp>
33cdf0e10cSrcweir #include <com/sun/star/script/XAllListener.hpp>
34cdf0e10cSrcweir #include <com/sun/star/script/XInvocationAdapterFactory.hpp>
35cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlReflection.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // InvocationToAllListenerMapper
38cdf0e10cSrcweir #include <com/sun/star/script/XInvocation.hpp>
39cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
40cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
41cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
42cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace com::sun::star::uno;
45cdf0e10cSrcweir using namespace com::sun::star::registry;
46cdf0e10cSrcweir using namespace com::sun::star::lang;
47cdf0e10cSrcweir using namespace com::sun::star::beans;
48cdf0e10cSrcweir using namespace com::sun::star::script;
49cdf0e10cSrcweir using namespace com::sun::star::reflection;
50cdf0e10cSrcweir using namespace cppu;
51cdf0e10cSrcweir using namespace osl;
52cdf0e10cSrcweir using namespace rtl;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir #define SERVICENAME "com.sun.star.script.EventAttacher"
56cdf0e10cSrcweir #define IMPLNAME    "com.sun.star.comp.EventAttacher"
57cdf0e10cSrcweir 
58cdf0e10cSrcweir namespace comp_EventAttacher {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //*************************************************************************
61cdf0e10cSrcweir //  class InvocationToAllListenerMapper
62cdf0e10cSrcweir //  helper class to map XInvocation to XAllListener
63cdf0e10cSrcweir //*************************************************************************
64cdf0e10cSrcweir class InvocationToAllListenerMapper : public WeakImplHelper1< XInvocation >
65cdf0e10cSrcweir {
66cdf0e10cSrcweir public:
67cdf0e10cSrcweir     InvocationToAllListenerMapper( const Reference< XIdlClass >& ListenerType,
68cdf0e10cSrcweir         const Reference< XAllListener >& AllListener, const Any& Helper );
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     // XInvocation
71cdf0e10cSrcweir     virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection(void) throw( RuntimeException );
72cdf0e10cSrcweir     virtual Any SAL_CALL invoke(const OUString& FunctionName, const Sequence< Any >& Params, Sequence< sal_Int16 >& OutParamIndex, Sequence< Any >& OutParam)
73cdf0e10cSrcweir         throw( IllegalArgumentException, CannotConvertException, InvocationTargetException, RuntimeException );
74cdf0e10cSrcweir     virtual void SAL_CALL setValue(const OUString& PropertyName, const Any& Value)
75cdf0e10cSrcweir         throw( UnknownPropertyException, CannotConvertException, InvocationTargetException, RuntimeException );
76cdf0e10cSrcweir     virtual Any SAL_CALL getValue(const OUString& PropertyName) throw( UnknownPropertyException, RuntimeException );
77cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasMethod(const OUString& Name) throw( RuntimeException );
78cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasProperty(const OUString& Name) throw( RuntimeException );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir private:
81cdf0e10cSrcweir     Reference< XIdlReflection >  m_xCoreReflection;
82cdf0e10cSrcweir     Reference< XAllListener >    m_xAllListener;
83cdf0e10cSrcweir     Reference< XIdlClass >       m_xListenerType;
84cdf0e10cSrcweir     Any                          m_Helper;
85cdf0e10cSrcweir };
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
88cdf0e10cSrcweir // Function to replace AllListenerAdapterService::createAllListerAdapter
createAllListenerAdapter(const Reference<XInvocationAdapterFactory> & xInvocationAdapterFactory,const Reference<XIdlClass> & xListenerType,const Reference<XAllListener> & xListener,const Any & Helper)89cdf0e10cSrcweir Reference< XInterface > createAllListenerAdapter
90cdf0e10cSrcweir (
91cdf0e10cSrcweir     const Reference< XInvocationAdapterFactory >& xInvocationAdapterFactory,
92cdf0e10cSrcweir     const Reference< XIdlClass >& xListenerType,
93cdf0e10cSrcweir     const Reference< XAllListener >& xListener,
94cdf0e10cSrcweir     const Any& Helper
95cdf0e10cSrcweir )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir     Reference< XInterface > xAdapter;
98cdf0e10cSrcweir     if( xInvocationAdapterFactory.is() && xListenerType.is() && xListener.is() )
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir        Reference< XInvocation > xInvocationToAllListenerMapper =
101cdf0e10cSrcweir             (XInvocation*)new InvocationToAllListenerMapper( xListenerType, xListener, Helper );
102cdf0e10cSrcweir         Type aListenerType( xListenerType->getTypeClass(), xListenerType->getName());
103cdf0e10cSrcweir         xAdapter = xInvocationAdapterFactory->createAdapter( xInvocationToAllListenerMapper, aListenerType );
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir     return xAdapter;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
109cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
110cdf0e10cSrcweir // InvocationToAllListenerMapper
InvocationToAllListenerMapper(const Reference<XIdlClass> & ListenerType,const Reference<XAllListener> & AllListener,const Any & Helper)111cdf0e10cSrcweir InvocationToAllListenerMapper::InvocationToAllListenerMapper
112cdf0e10cSrcweir     ( const Reference< XIdlClass >& ListenerType, const Reference< XAllListener >& AllListener, const Any& Helper )
113cdf0e10cSrcweir         : m_xAllListener( AllListener )
114cdf0e10cSrcweir         , m_xListenerType( ListenerType )
115cdf0e10cSrcweir         , m_Helper( Helper )
116cdf0e10cSrcweir {
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir //*************************************************************************
getIntrospection(void)120cdf0e10cSrcweir Reference< XIntrospectionAccess > SAL_CALL InvocationToAllListenerMapper::getIntrospection(void)
121cdf0e10cSrcweir     throw( RuntimeException )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     return Reference< XIntrospectionAccess >();
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //*************************************************************************
invoke(const OUString & FunctionName,const Sequence<Any> & Params,Sequence<sal_Int16> &,Sequence<Any> &)127cdf0e10cSrcweir Any SAL_CALL InvocationToAllListenerMapper::invoke(const OUString& FunctionName, const Sequence< Any >& Params,
128cdf0e10cSrcweir     Sequence< sal_Int16 >& , Sequence< Any >& )
129cdf0e10cSrcweir         throw( IllegalArgumentException, CannotConvertException,
130cdf0e10cSrcweir         InvocationTargetException, RuntimeException )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     Any aRet;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     // Check if to firing or approveFiring has to be called
135cdf0e10cSrcweir     Reference< XIdlMethod > xMethod = m_xListenerType->getMethod( FunctionName );
136cdf0e10cSrcweir     sal_Bool bApproveFiring = sal_False;
137cdf0e10cSrcweir     if( !xMethod.is() )
138cdf0e10cSrcweir         return aRet;
139cdf0e10cSrcweir     Reference< XIdlClass > xReturnType = xMethod->getReturnType();
140cdf0e10cSrcweir     Sequence< Reference< XIdlClass > > aExceptionSeq = xMethod->getExceptionTypes();
141cdf0e10cSrcweir     if( ( xReturnType.is() && xReturnType->getTypeClass() != TypeClass_VOID ) ||
142cdf0e10cSrcweir         aExceptionSeq.getLength() > 0 )
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         bApproveFiring = sal_True;
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir     else
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         Sequence< ParamInfo > aParamSeq = xMethod->getParameterInfos();
149cdf0e10cSrcweir         sal_uInt32 nParamCount = aParamSeq.getLength();
150cdf0e10cSrcweir         if( nParamCount > 1 )
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             const ParamInfo* pInfos = aParamSeq.getConstArray();
153cdf0e10cSrcweir             for( sal_uInt32 i = 0 ; i < nParamCount ; i++ )
154cdf0e10cSrcweir             {
155cdf0e10cSrcweir                 if( pInfos[ i ].aMode != ParamMode_IN )
156cdf0e10cSrcweir                 {
157cdf0e10cSrcweir                     bApproveFiring = sal_True;
158cdf0e10cSrcweir                     break;
159cdf0e10cSrcweir                 }
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     AllEventObject aAllEvent;
165cdf0e10cSrcweir     aAllEvent.Source = (OWeakObject*) this;
166cdf0e10cSrcweir     aAllEvent.Helper = m_Helper;
167cdf0e10cSrcweir     aAllEvent.ListenerType = Type(m_xListenerType->getTypeClass(), m_xListenerType->getName());
168cdf0e10cSrcweir     aAllEvent.MethodName = FunctionName;
169cdf0e10cSrcweir     aAllEvent.Arguments = Params;
170cdf0e10cSrcweir     if( bApproveFiring )
171cdf0e10cSrcweir         aRet = m_xAllListener->approveFiring( aAllEvent );
172cdf0e10cSrcweir     else
173cdf0e10cSrcweir         m_xAllListener->firing( aAllEvent );
174cdf0e10cSrcweir     return aRet;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir //*************************************************************************
setValue(const OUString &,const Any &)178cdf0e10cSrcweir void SAL_CALL InvocationToAllListenerMapper::setValue(const OUString& , const Any& )
179cdf0e10cSrcweir     throw( UnknownPropertyException, CannotConvertException,
180cdf0e10cSrcweir            InvocationTargetException, RuntimeException )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir //*************************************************************************
getValue(const OUString &)185cdf0e10cSrcweir Any SAL_CALL InvocationToAllListenerMapper::getValue(const OUString& )
186cdf0e10cSrcweir     throw( UnknownPropertyException, RuntimeException )
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     return Any();
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir //*************************************************************************
hasMethod(const OUString & Name)192cdf0e10cSrcweir sal_Bool SAL_CALL InvocationToAllListenerMapper::hasMethod(const OUString& Name)
193cdf0e10cSrcweir     throw( RuntimeException )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir     Reference< XIdlMethod > xMethod = m_xListenerType->getMethod( Name );
196cdf0e10cSrcweir     return xMethod.is();
197cdf0e10cSrcweir }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir //*************************************************************************
hasProperty(const OUString & Name)200cdf0e10cSrcweir sal_Bool SAL_CALL InvocationToAllListenerMapper::hasProperty(const OUString& Name)
201cdf0e10cSrcweir     throw( RuntimeException )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir     Reference< XIdlField > xField = m_xListenerType->getField( Name );
204cdf0e10cSrcweir     return xField.is();
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir //*************************************************************************
208cdf0e10cSrcweir //  class EventAttacherImpl
209cdf0e10cSrcweir //  represents an implementation of the EventAttacher service
210cdf0e10cSrcweir //*************************************************************************
211cdf0e10cSrcweir class EventAttacherImpl : public WeakImplHelper3 < XEventAttacher, XInitialization, XServiceInfo >
212cdf0e10cSrcweir {
213cdf0e10cSrcweir public:
214cdf0e10cSrcweir     EventAttacherImpl( const Reference< XMultiServiceFactory >& );
215cdf0e10cSrcweir     ~EventAttacherImpl();
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     // XServiceInfo
218cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
219cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
220cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
221cdf0e10cSrcweir     static OUString SAL_CALL getImplementationName_Static(  );
222cdf0e10cSrcweir     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir     // XInitialization
225cdf0e10cSrcweir     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
226cdf0e10cSrcweir         throw( Exception, RuntimeException);
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     // Methoden von XEventAttacher
229cdf0e10cSrcweir     virtual Reference< XEventListener > SAL_CALL attachListener(const Reference< XInterface >& xObject,
230cdf0e10cSrcweir             const Reference< XAllListener >& AllListener, const Any& Helper,
231cdf0e10cSrcweir             const OUString& ListenerType, const OUString& AddListenerParam)
232cdf0e10cSrcweir         throw( IllegalArgumentException, ServiceNotRegisteredException, CannotCreateAdapterException, IntrospectionException, RuntimeException );
233cdf0e10cSrcweir     virtual Reference< XEventListener > SAL_CALL attachSingleEventListener(const Reference< XInterface >& xObject,
234cdf0e10cSrcweir             const Reference< XAllListener >& AllListener, const Any& Helper,
235cdf0e10cSrcweir             const OUString& ListenerType, const OUString& AddListenerParam,
236cdf0e10cSrcweir             const OUString& EventMethod)
237cdf0e10cSrcweir         throw( IllegalArgumentException, ServiceNotRegisteredException, CannotCreateAdapterException, IntrospectionException, RuntimeException );
238cdf0e10cSrcweir     virtual void SAL_CALL removeListener(const Reference< XInterface >& xObject,
239cdf0e10cSrcweir             const OUString& ListenerType, const OUString& AddListenerParam,
240cdf0e10cSrcweir             const Reference< XEventListener >& aToRemoveListener)
241cdf0e10cSrcweir         throw( IllegalArgumentException, IntrospectionException, RuntimeException );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     // used by FilterAllListener_Impl
244cdf0e10cSrcweir     Reference< XTypeConverter > getConverter() throw( Exception );
245cdf0e10cSrcweir 
246cdf0e10cSrcweir     friend class FilterAllListenerImpl;
247cdf0e10cSrcweir private:
248cdf0e10cSrcweir     Mutex                               m_aMutex;
249cdf0e10cSrcweir     Reference< XMultiServiceFactory >   m_xSMgr;
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     // Services merken
252cdf0e10cSrcweir     Reference< XIntrospection >             m_xIntrospection;
253cdf0e10cSrcweir     Reference< XIdlReflection >             m_xReflection;
254cdf0e10cSrcweir     Reference< XTypeConverter >             m_xConverter;
255cdf0e10cSrcweir     Reference< XInvocationAdapterFactory >  m_xInvocationAdapterFactory;
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     // needed services
258cdf0e10cSrcweir     Reference< XIntrospection >             getIntrospection() throw( Exception );
259cdf0e10cSrcweir     Reference< XIdlReflection >             getReflection() throw( Exception );
260cdf0e10cSrcweir     Reference< XInvocationAdapterFactory >  getInvocationAdapterService() throw( Exception );
261cdf0e10cSrcweir };
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 
264cdf0e10cSrcweir //*************************************************************************
EventAttacherImpl(const Reference<XMultiServiceFactory> & rSMgr)265cdf0e10cSrcweir EventAttacherImpl::EventAttacherImpl( const Reference< XMultiServiceFactory >& rSMgr )
266cdf0e10cSrcweir     : m_xSMgr( rSMgr )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir //*************************************************************************
~EventAttacherImpl()271cdf0e10cSrcweir EventAttacherImpl::~EventAttacherImpl()
272cdf0e10cSrcweir {
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir //*************************************************************************
EventAttacherImpl_CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)276cdf0e10cSrcweir Reference< XInterface > SAL_CALL EventAttacherImpl_CreateInstance( const Reference< XMultiServiceFactory >& rSMgr ) throw( Exception )
277cdf0e10cSrcweir {
278cdf0e10cSrcweir     Reference< XInterface > xRet;
279cdf0e10cSrcweir     XEventAttacher *pEventAttacher = (XEventAttacher*) new EventAttacherImpl(rSMgr);
280cdf0e10cSrcweir 
281cdf0e10cSrcweir     if (pEventAttacher)
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir         xRet = Reference<XInterface>::query(pEventAttacher);
284cdf0e10cSrcweir     }
285cdf0e10cSrcweir 
286cdf0e10cSrcweir     return xRet;
287cdf0e10cSrcweir }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir //*************************************************************************
getImplementationName()290cdf0e10cSrcweir OUString SAL_CALL EventAttacherImpl::getImplementationName(  )
291cdf0e10cSrcweir     throw(RuntimeException)
292cdf0e10cSrcweir {
293cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) );
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir //*************************************************************************
supportsService(const OUString & ServiceName)297cdf0e10cSrcweir sal_Bool SAL_CALL EventAttacherImpl::supportsService( const OUString& ServiceName )
298cdf0e10cSrcweir     throw(RuntimeException)
299cdf0e10cSrcweir {
300cdf0e10cSrcweir     Sequence< OUString > aSNL = getSupportedServiceNames();
301cdf0e10cSrcweir     const OUString * pArray = aSNL.getArray();
302cdf0e10cSrcweir     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
303cdf0e10cSrcweir         if( pArray[i] == ServiceName )
304cdf0e10cSrcweir             return sal_True;
305cdf0e10cSrcweir     return sal_False;
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames()309cdf0e10cSrcweir Sequence<OUString> SAL_CALL EventAttacherImpl::getSupportedServiceNames(  )
310cdf0e10cSrcweir     throw(RuntimeException)
311cdf0e10cSrcweir {
312cdf0e10cSrcweir     return getSupportedServiceNames_Static();
313cdf0e10cSrcweir }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames_Static()316cdf0e10cSrcweir Sequence<OUString> SAL_CALL EventAttacherImpl::getSupportedServiceNames_Static(  )
317cdf0e10cSrcweir {
318cdf0e10cSrcweir     OUString aStr( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME ) );
319cdf0e10cSrcweir     return Sequence< OUString >( &aStr, 1 );
320cdf0e10cSrcweir }
321cdf0e10cSrcweir 
322cdf0e10cSrcweir //*************************************************************************
initialize(const Sequence<Any> & Arguments)323cdf0e10cSrcweir void SAL_CALL EventAttacherImpl::initialize(const Sequence< Any >& Arguments) throw( Exception, RuntimeException )
324cdf0e10cSrcweir {
325cdf0e10cSrcweir     // get services from the argument list
326cdf0e10cSrcweir     const Any * pArray = Arguments.getConstArray();
327cdf0e10cSrcweir     for( sal_Int32 i = 0; i < Arguments.getLength(); i++ )
328cdf0e10cSrcweir     {
329cdf0e10cSrcweir         if( pArray[i].getValueType().getTypeClass() != TypeClass_INTERFACE )
330cdf0e10cSrcweir             throw IllegalArgumentException();
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         // InvocationAdapter service ?
333cdf0e10cSrcweir         Reference< XInvocationAdapterFactory > xALAS;
334cdf0e10cSrcweir         pArray[i] >>= xALAS;
335cdf0e10cSrcweir         if( xALAS.is() )
336cdf0e10cSrcweir         {
337cdf0e10cSrcweir             Guard< Mutex > aGuard( m_aMutex );
338cdf0e10cSrcweir             m_xInvocationAdapterFactory = xALAS;
339cdf0e10cSrcweir         }
340cdf0e10cSrcweir         // Introspection service ?
341cdf0e10cSrcweir         Reference< XIntrospection > xI;
342cdf0e10cSrcweir         pArray[i] >>= xI;
343cdf0e10cSrcweir         if( xI.is() )
344cdf0e10cSrcweir         {
345cdf0e10cSrcweir             Guard< Mutex > aGuard( m_aMutex );
346cdf0e10cSrcweir             m_xIntrospection = xI;
347cdf0e10cSrcweir         }
348cdf0e10cSrcweir         // Reflection service ?
349cdf0e10cSrcweir         Reference< XIdlReflection > xIdlR;
350cdf0e10cSrcweir         pArray[i] >>= xIdlR;
351cdf0e10cSrcweir         if( xIdlR.is() )
352cdf0e10cSrcweir         {
353cdf0e10cSrcweir             Guard< Mutex > aGuard( m_aMutex );
354cdf0e10cSrcweir             m_xReflection = xIdlR;
355cdf0e10cSrcweir         }
356cdf0e10cSrcweir         // Converter Service ?
357cdf0e10cSrcweir         Reference< XTypeConverter > xC;
358cdf0e10cSrcweir         pArray[i] >>= xC;
359cdf0e10cSrcweir         if( xC.is() )
360cdf0e10cSrcweir         {
361cdf0e10cSrcweir             Guard< Mutex > aGuard( m_aMutex );
362cdf0e10cSrcweir             m_xConverter = xC;
363cdf0e10cSrcweir         }
364cdf0e10cSrcweir 
365cdf0e10cSrcweir         // no right interface
366cdf0e10cSrcweir         if( !xALAS.is() && !xI.is() && !xIdlR.is() && !xC.is() )
367cdf0e10cSrcweir             throw IllegalArgumentException();
368cdf0e10cSrcweir     }
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
371cdf0e10cSrcweir //*************************************************************************
372cdf0e10cSrcweir //*** Private Hilfs-Methoden ***
getIntrospection()373cdf0e10cSrcweir Reference< XIntrospection > EventAttacherImpl::getIntrospection() throw( Exception )
374cdf0e10cSrcweir {
375cdf0e10cSrcweir     Guard< Mutex > aGuard( m_aMutex );
376cdf0e10cSrcweir     // Haben wir den Service schon? Sonst anlegen
377cdf0e10cSrcweir     if( !m_xIntrospection.is() )
378cdf0e10cSrcweir     {
379cdf0e10cSrcweir         Reference< XInterface > xIFace( m_xSMgr->createInstance( rtl::OUString::createFromAscii("com.sun.star.beans.Introspection") ) );
380cdf0e10cSrcweir         m_xIntrospection = Reference< XIntrospection >( xIFace, UNO_QUERY );
381cdf0e10cSrcweir     }
382cdf0e10cSrcweir     return m_xIntrospection;
383cdf0e10cSrcweir }
384cdf0e10cSrcweir 
385cdf0e10cSrcweir //*************************************************************************
386cdf0e10cSrcweir //*** Private Hilfs-Methoden ***
getReflection()387cdf0e10cSrcweir Reference< XIdlReflection > EventAttacherImpl::getReflection() throw( Exception )
388cdf0e10cSrcweir {
389cdf0e10cSrcweir     Guard< Mutex > aGuard( m_aMutex );
390cdf0e10cSrcweir     // Haben wir den Service schon? Sonst anlegen
391cdf0e10cSrcweir     if( !m_xReflection.is() )
392cdf0e10cSrcweir     {
393cdf0e10cSrcweir         Reference< XInterface > xIFace( m_xSMgr->createInstance( rtl::OUString::createFromAscii("com.sun.star.reflection.CoreReflection") ) );
394cdf0e10cSrcweir         m_xReflection = Reference< XIdlReflection >( xIFace, UNO_QUERY);
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir     return m_xReflection;
397cdf0e10cSrcweir }
398cdf0e10cSrcweir 
399cdf0e10cSrcweir //*************************************************************************
400cdf0e10cSrcweir //*** Private Hilfs-Methoden ***
getInvocationAdapterService()401cdf0e10cSrcweir Reference< XInvocationAdapterFactory > EventAttacherImpl::getInvocationAdapterService() throw( Exception )
402cdf0e10cSrcweir {
403cdf0e10cSrcweir     Guard< Mutex > aGuard( m_aMutex );
404cdf0e10cSrcweir     // Haben wir den Service schon? Sonst anlegen
405cdf0e10cSrcweir     if( !m_xInvocationAdapterFactory.is() )
406cdf0e10cSrcweir     {
407cdf0e10cSrcweir         Reference< XInterface > xIFace( m_xSMgr->createInstance( rtl::OUString::createFromAscii("com.sun.star.script.InvocationAdapterFactory") ) );
408cdf0e10cSrcweir         m_xInvocationAdapterFactory = Reference< XInvocationAdapterFactory >( xIFace, UNO_QUERY );
409cdf0e10cSrcweir     }
410cdf0e10cSrcweir     return m_xInvocationAdapterFactory;
411cdf0e10cSrcweir }
412cdf0e10cSrcweir 
413cdf0e10cSrcweir 
414cdf0e10cSrcweir //*************************************************************************
415cdf0e10cSrcweir //*** Private Hilfs-Methoden ***
getConverter()416cdf0e10cSrcweir Reference< XTypeConverter > EventAttacherImpl::getConverter() throw( Exception )
417cdf0e10cSrcweir {
418cdf0e10cSrcweir     Guard< Mutex > aGuard( m_aMutex );
419cdf0e10cSrcweir     // Haben wir den Service schon? Sonst anlegen
420cdf0e10cSrcweir     if( !m_xConverter.is() )
421cdf0e10cSrcweir     {
422cdf0e10cSrcweir         Reference< XInterface > xIFace( m_xSMgr->createInstance( rtl::OUString::createFromAscii("com.sun.star.script.Converter") ) );
423cdf0e10cSrcweir         m_xConverter = Reference< XTypeConverter >( xIFace, UNO_QUERY );
424cdf0e10cSrcweir     }
425cdf0e10cSrcweir     return m_xConverter;
426cdf0e10cSrcweir }
427cdf0e10cSrcweir 
428cdf0e10cSrcweir //------------------------------------------------------------------------
429cdf0e10cSrcweir //------------------------------------------------------------------------
430cdf0e10cSrcweir //------------------------------------------------------------------------
431cdf0e10cSrcweir // Implementation eines EventAttacher-bezogenen AllListeners, der
432cdf0e10cSrcweir // nur einzelne Events an einen allgemeinen AllListener weiterleitet
433cdf0e10cSrcweir class FilterAllListenerImpl : public WeakImplHelper1< XAllListener  >
434cdf0e10cSrcweir {
435cdf0e10cSrcweir public:
436cdf0e10cSrcweir     FilterAllListenerImpl( EventAttacherImpl * pEA_, const OUString& EventMethod_,
437cdf0e10cSrcweir                            const Reference< XAllListener >& AllListener_ );
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     // XAllListener
440cdf0e10cSrcweir     virtual void SAL_CALL firing(const AllEventObject& Event) throw( RuntimeException );
441cdf0e10cSrcweir     virtual Any SAL_CALL approveFiring(const AllEventObject& Event) throw( InvocationTargetException, RuntimeException );
442cdf0e10cSrcweir 
443cdf0e10cSrcweir     // XEventListener
444cdf0e10cSrcweir     virtual void SAL_CALL disposing(const EventObject& Source) throw( RuntimeException );
445cdf0e10cSrcweir 
446cdf0e10cSrcweir private:
447cdf0e10cSrcweir     // convert
448cdf0e10cSrcweir     void convertToEventReturn( Any & rRet, const Type& rRetType )
449cdf0e10cSrcweir             throw( CannotConvertException );
450cdf0e10cSrcweir 
451cdf0e10cSrcweir     EventAttacherImpl *         m_pEA;
452cdf0e10cSrcweir     Reference< XInterface >     m_xEAHold;
453cdf0e10cSrcweir     OUString                    m_EventMethod;
454cdf0e10cSrcweir     Reference< XAllListener >   m_AllListener;
455cdf0e10cSrcweir };
456cdf0e10cSrcweir 
457cdf0e10cSrcweir //*************************************************************************
FilterAllListenerImpl(EventAttacherImpl * pEA_,const OUString & EventMethod_,const Reference<XAllListener> & AllListener_)458cdf0e10cSrcweir FilterAllListenerImpl::FilterAllListenerImpl( EventAttacherImpl * pEA_, const OUString& EventMethod_,
459cdf0e10cSrcweir                                               const Reference< XAllListener >& AllListener_ )
460cdf0e10cSrcweir         : m_pEA( pEA_ )
461cdf0e10cSrcweir         , m_xEAHold( *pEA_ )
462cdf0e10cSrcweir         , m_EventMethod( EventMethod_ )
463cdf0e10cSrcweir         , m_AllListener( AllListener_ )
464cdf0e10cSrcweir {
465cdf0e10cSrcweir }
466cdf0e10cSrcweir 
467cdf0e10cSrcweir //*************************************************************************
firing(const AllEventObject & Event)468cdf0e10cSrcweir void SAL_CALL FilterAllListenerImpl::firing(const AllEventObject& Event)
469cdf0e10cSrcweir     throw( RuntimeException )
470cdf0e10cSrcweir {
471cdf0e10cSrcweir     // Nur durchreichen, wenn es die richtige Methode ist
472cdf0e10cSrcweir     if( Event.MethodName == m_EventMethod && m_AllListener.is() )
473cdf0e10cSrcweir         m_AllListener->firing( Event );
474cdf0e10cSrcweir }
475cdf0e10cSrcweir 
476cdf0e10cSrcweir //*************************************************************************
477cdf0e10cSrcweir // Convert to the standard event return
convertToEventReturn(Any & rRet,const Type & rRetType)478cdf0e10cSrcweir void FilterAllListenerImpl::convertToEventReturn( Any & rRet, const Type & rRetType )
479cdf0e10cSrcweir     throw( CannotConvertException )
480cdf0e10cSrcweir {
481cdf0e10cSrcweir     // no return value? Set to the specified values
482cdf0e10cSrcweir     if( rRet.getValueType().getTypeClass() == TypeClass_VOID )
483cdf0e10cSrcweir     {
484cdf0e10cSrcweir         switch( rRetType.getTypeClass()  )
485cdf0e10cSrcweir         {
486cdf0e10cSrcweir             case TypeClass_INTERFACE:
487cdf0e10cSrcweir                 {
488cdf0e10cSrcweir                 rRet <<= Reference< XInterface >();
489cdf0e10cSrcweir                 }
490cdf0e10cSrcweir                 break;
491cdf0e10cSrcweir 
492cdf0e10cSrcweir             case TypeClass_BOOLEAN:
493cdf0e10cSrcweir                 rRet <<= sal_True;
494cdf0e10cSrcweir                 break;
495cdf0e10cSrcweir 
496cdf0e10cSrcweir             case TypeClass_STRING:
497cdf0e10cSrcweir                 rRet <<= OUString();
498cdf0e10cSrcweir                 break;
499cdf0e10cSrcweir 
500cdf0e10cSrcweir             case TypeClass_FLOAT:           rRet <<= float(0);  break;
501cdf0e10cSrcweir             case TypeClass_DOUBLE:          rRet <<= double(0.0);   break;
502cdf0e10cSrcweir             case TypeClass_BYTE:            rRet <<= sal_uInt8( 0 );    break;
503cdf0e10cSrcweir             case TypeClass_SHORT:           rRet <<= sal_Int16( 0 );    break;
504cdf0e10cSrcweir             case TypeClass_LONG:            rRet <<= sal_Int32( 0 );    break;
505cdf0e10cSrcweir             case TypeClass_UNSIGNED_SHORT:  rRet <<= sal_uInt16( 0 );   break;
506cdf0e10cSrcweir             case TypeClass_UNSIGNED_LONG:   rRet <<= sal_uInt32( 0 );   break;
507cdf0e10cSrcweir                      default:
508cdf0e10cSrcweir             break;
509cdf0e10cSrcweir         }
510cdf0e10cSrcweir     }
511cdf0e10cSrcweir     else if( !rRet.getValueType().equals( rRetType ) )
512cdf0e10cSrcweir     {
513cdf0e10cSrcweir         Reference< XTypeConverter > xConverter = m_pEA->getConverter();
514cdf0e10cSrcweir         if( xConverter.is() )
515cdf0e10cSrcweir             rRet = xConverter->convertTo( rRet, rRetType );
516cdf0e10cSrcweir         else
517cdf0e10cSrcweir             throw CannotConvertException(); // TODO TypeConversionException
518cdf0e10cSrcweir     }
519cdf0e10cSrcweir }
520cdf0e10cSrcweir 
521cdf0e10cSrcweir //*************************************************************************
approveFiring(const AllEventObject & Event)522cdf0e10cSrcweir Any SAL_CALL FilterAllListenerImpl::approveFiring( const AllEventObject& Event )
523cdf0e10cSrcweir     throw( InvocationTargetException, RuntimeException )
524cdf0e10cSrcweir {
525cdf0e10cSrcweir     Any aRet;
526cdf0e10cSrcweir 
527cdf0e10cSrcweir     // Nur durchreichen, wenn es die richtige Methode ist
528cdf0e10cSrcweir     if( Event.MethodName == m_EventMethod && m_AllListener.is() )
529cdf0e10cSrcweir         aRet = m_AllListener->approveFiring( Event );
530cdf0e10cSrcweir     else
531cdf0e10cSrcweir     {
532cdf0e10cSrcweir         // Convert to the standard event return
533cdf0e10cSrcweir         try
534cdf0e10cSrcweir         {
535cdf0e10cSrcweir             Reference< XIdlClass > xListenerType = m_pEA->getReflection()->
536cdf0e10cSrcweir                         forName( Event.ListenerType.getTypeName() );
537cdf0e10cSrcweir             Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
538cdf0e10cSrcweir             if( xMeth.is() )
539cdf0e10cSrcweir             {
540cdf0e10cSrcweir                 Reference< XIdlClass > xRetType = xMeth->getReturnType();
541cdf0e10cSrcweir                 Type aRetType( xRetType->getTypeClass(), xRetType->getName() );
542cdf0e10cSrcweir                 convertToEventReturn( aRet, aRetType );
543cdf0e10cSrcweir             }
544cdf0e10cSrcweir         }
545cdf0e10cSrcweir         catch( CannotConvertException& e )
546cdf0e10cSrcweir         {
547cdf0e10cSrcweir             throw InvocationTargetException( OUString(), Reference< XInterface >(), Any(&e, ::getCppuType( (CannotConvertException*)0)) );
548cdf0e10cSrcweir         }
549cdf0e10cSrcweir     }
550cdf0e10cSrcweir     return aRet;
551cdf0e10cSrcweir }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir //*************************************************************************
disposing(const EventObject &)554cdf0e10cSrcweir void FilterAllListenerImpl::disposing(const EventObject& )
555cdf0e10cSrcweir     throw( RuntimeException )
556cdf0e10cSrcweir {
557cdf0e10cSrcweir     // TODO: ???
558cdf0e10cSrcweir }
559cdf0e10cSrcweir 
560cdf0e10cSrcweir 
561cdf0e10cSrcweir //*************************************************************************
attachListener(const Reference<XInterface> & xObject,const Reference<XAllListener> & AllListener,const Any & Helper,const OUString & ListenerType,const OUString & AddListenerParam)562cdf0e10cSrcweir Reference< XEventListener > EventAttacherImpl::attachListener
563cdf0e10cSrcweir (
564cdf0e10cSrcweir     const Reference< XInterface >& xObject,
565cdf0e10cSrcweir     const Reference< XAllListener >& AllListener,
566cdf0e10cSrcweir     const Any& Helper,
567cdf0e10cSrcweir     const OUString& ListenerType,
568cdf0e10cSrcweir     const OUString& AddListenerParam
569cdf0e10cSrcweir )
570cdf0e10cSrcweir     throw( IllegalArgumentException, ServiceNotRegisteredException, CannotCreateAdapterException, IntrospectionException, RuntimeException )
571cdf0e10cSrcweir {
572cdf0e10cSrcweir     if( !xObject.is() || !AllListener.is() )
573cdf0e10cSrcweir         throw IllegalArgumentException();
574cdf0e10cSrcweir 
575cdf0e10cSrcweir     Reference< XEventListener > xRet = NULL;
576cdf0e10cSrcweir 
577cdf0e10cSrcweir     // InvocationAdapterService holen
578cdf0e10cSrcweir     Reference< XInvocationAdapterFactory > xInvocationAdapterFactory = getInvocationAdapterService();
579cdf0e10cSrcweir     if( !xInvocationAdapterFactory.is() )
580cdf0e10cSrcweir         throw ServiceNotRegisteredException();
581cdf0e10cSrcweir 
582cdf0e10cSrcweir     // Listener-Klasse per Reflection besorgen
583cdf0e10cSrcweir     Reference< XIdlReflection > xReflection = getReflection();
584cdf0e10cSrcweir     if( !xReflection.is() )
585cdf0e10cSrcweir         throw ServiceNotRegisteredException();
586cdf0e10cSrcweir 
587cdf0e10cSrcweir     // Anmelden, dazu passende addListener-Methode aufrufen
588cdf0e10cSrcweir     // Zunaechst ueber Introspection gehen, da die Methoden in der gleichen
589cdf0e10cSrcweir     // Weise analysiert werden koennen. Fuer bessere Performance entweder
590cdf0e10cSrcweir     // hier nochmal implementieren oder die Impl-Methode der Introspection
591cdf0e10cSrcweir     // fuer diesen Zweck konfigurierbar machen.
592cdf0e10cSrcweir 
593cdf0e10cSrcweir     // Introspection-Service holen
594cdf0e10cSrcweir     Reference< XIntrospection > xIntrospection = getIntrospection();
595cdf0e10cSrcweir     if( !xIntrospection.is() )
596cdf0e10cSrcweir         return xRet;
597cdf0e10cSrcweir 
598cdf0e10cSrcweir     // und unspecten
599cdf0e10cSrcweir     Any aObjAny( &xObject, ::getCppuType( (const Reference< XInterface > *)0) );
600cdf0e10cSrcweir 
601cdf0e10cSrcweir     Reference< XIntrospectionAccess > xAccess = xIntrospection->inspect( aObjAny );
602cdf0e10cSrcweir     if( !xAccess.is() )
603cdf0e10cSrcweir         return xRet;
604cdf0e10cSrcweir 
605cdf0e10cSrcweir     // Name der addListener-Methode zusammenbasteln
606cdf0e10cSrcweir     OUString aAddListenerName;
607cdf0e10cSrcweir     OUString aListenerName( ListenerType );
608cdf0e10cSrcweir     sal_Int32 nIndex = aListenerName.lastIndexOf( '.' );
609cdf0e10cSrcweir     // set index to the interface name without package name
610cdf0e10cSrcweir     if( nIndex == -1 )
611cdf0e10cSrcweir         // not found
612cdf0e10cSrcweir         nIndex = 0;
613cdf0e10cSrcweir     else
614cdf0e10cSrcweir         nIndex++;
615cdf0e10cSrcweir     if( aListenerName[nIndex] == 'X' )
616cdf0e10cSrcweir         // erase X from the interface name
617cdf0e10cSrcweir         aListenerName = aListenerName.copy( nIndex +1 );
618cdf0e10cSrcweir     aAddListenerName = OUString( RTL_CONSTASCII_USTRINGPARAM( "add" ) ) + aListenerName;
619cdf0e10cSrcweir 
620cdf0e10cSrcweir     // Methoden nach der passenden addListener-Methode durchsuchen
621cdf0e10cSrcweir     Sequence< Reference< XIdlMethod > > aMethodSeq = xAccess->getMethods( MethodConcept::LISTENER );
622cdf0e10cSrcweir     sal_uInt32 i, nLen = aMethodSeq.getLength();
623cdf0e10cSrcweir     const Reference< XIdlMethod >* pMethods = aMethodSeq.getConstArray();
624cdf0e10cSrcweir 
625cdf0e10cSrcweir     for( i = 0 ; i < nLen ; i++ )
626cdf0e10cSrcweir     {
627cdf0e10cSrcweir         // Methode ansprechen
628cdf0e10cSrcweir         const Reference< XIdlMethod >& rxMethod = pMethods[i];
629cdf0e10cSrcweir 
630cdf0e10cSrcweir         // Ist es die richtige Methode?
631cdf0e10cSrcweir         OUString aMethName = rxMethod->getName();
632cdf0e10cSrcweir 
633cdf0e10cSrcweir         if( aAddListenerName == aMethName )
634cdf0e10cSrcweir         {
635cdf0e10cSrcweir             Sequence< Reference< XIdlClass > > params = rxMethod->getParameterTypes();
636cdf0e10cSrcweir             sal_uInt32 nParamCount = params.getLength();
637cdf0e10cSrcweir 
638cdf0e10cSrcweir             Reference< XIdlClass > xListenerType;
639cdf0e10cSrcweir             if( nParamCount == 1 )
640cdf0e10cSrcweir                 xListenerType = params.getConstArray()[0];
641cdf0e10cSrcweir             else if( nParamCount == 2 )
642cdf0e10cSrcweir                 xListenerType = params.getConstArray()[1];
643cdf0e10cSrcweir 
644cdf0e10cSrcweir             // Adapter zum eigentlichen Listener-Typ anfordern
645cdf0e10cSrcweir             Reference< XInterface > xAdapter = createAllListenerAdapter
646cdf0e10cSrcweir                 ( xInvocationAdapterFactory, xListenerType, AllListener, Helper );
647cdf0e10cSrcweir 
648cdf0e10cSrcweir             if( !xAdapter.is() )
649cdf0e10cSrcweir                 throw CannotCreateAdapterException();
650cdf0e10cSrcweir             xRet = Reference< XEventListener >( xAdapter, UNO_QUERY );
651cdf0e10cSrcweir 
652cdf0e10cSrcweir 
653cdf0e10cSrcweir             // Nur der Listener als Parameter?
654cdf0e10cSrcweir             if( nParamCount == 1 )
655cdf0e10cSrcweir             {
656cdf0e10cSrcweir                 Sequence< Any > args( 1 );
657cdf0e10cSrcweir                 args.getArray()[0] <<= xAdapter;
658cdf0e10cSrcweir                 try
659cdf0e10cSrcweir                 {
660cdf0e10cSrcweir                     rxMethod->invoke( aObjAny, args );
661cdf0e10cSrcweir                 }
662cdf0e10cSrcweir                 catch( InvocationTargetException& )
663cdf0e10cSrcweir                 {
664cdf0e10cSrcweir                     throw IntrospectionException();
665cdf0e10cSrcweir                 }
666cdf0e10cSrcweir             }
667cdf0e10cSrcweir             // Sonst den Zusatzparameter mit uebergeben
668cdf0e10cSrcweir             else if( nParamCount == 2 )
669cdf0e10cSrcweir             {
670cdf0e10cSrcweir                 Sequence< Any > args( 2 );
671cdf0e10cSrcweir                 Any* pAnys = args.getArray();
672cdf0e10cSrcweir 
673cdf0e10cSrcweir                 // Typ des 1. Parameters pruefen
674cdf0e10cSrcweir                 Reference< XIdlClass > xParamClass = params.getConstArray()[0];
675cdf0e10cSrcweir                 if( xParamClass->getTypeClass() == TypeClass_STRING )
676cdf0e10cSrcweir                 {
677cdf0e10cSrcweir                     pAnys[0] <<= AddListenerParam;
678cdf0e10cSrcweir                 }
679cdf0e10cSrcweir 
680cdf0e10cSrcweir                 // 2. Parameter == Listener? TODO: Pruefen!
681cdf0e10cSrcweir                 pAnys[1] <<= xAdapter;
682cdf0e10cSrcweir 
683cdf0e10cSrcweir                 // TODO: Konvertierung String -> ?
684cdf0e10cSrcweir                 // else
685cdf0e10cSrcweir                 try
686cdf0e10cSrcweir                 {
687cdf0e10cSrcweir                     rxMethod->invoke( aObjAny, args );
688cdf0e10cSrcweir                 }
689cdf0e10cSrcweir                 catch( InvocationTargetException& )
690cdf0e10cSrcweir                 {
691cdf0e10cSrcweir                     throw IntrospectionException();
692cdf0e10cSrcweir                 }
693cdf0e10cSrcweir             }
694cdf0e10cSrcweir             break;
695cdf0e10cSrcweir             // else...
696cdf0e10cSrcweir             // Alles andere wird nicht unterstuetzt
697cdf0e10cSrcweir         }
698cdf0e10cSrcweir     }
699cdf0e10cSrcweir 
700cdf0e10cSrcweir     return xRet;
701cdf0e10cSrcweir }
702cdf0e10cSrcweir 
703cdf0e10cSrcweir // XEventAttacher
attachSingleEventListener(const Reference<XInterface> & xObject,const Reference<XAllListener> & AllListener,const Any & Helper,const OUString & ListenerType,const OUString & AddListenerParam,const OUString & EventMethod)704cdf0e10cSrcweir Reference< XEventListener > EventAttacherImpl::attachSingleEventListener
705cdf0e10cSrcweir (
706cdf0e10cSrcweir     const Reference< XInterface >& xObject,
707cdf0e10cSrcweir     const Reference< XAllListener >& AllListener,
708cdf0e10cSrcweir     const Any& Helper,
709cdf0e10cSrcweir     const OUString& ListenerType,
710cdf0e10cSrcweir     const OUString& AddListenerParam,
711cdf0e10cSrcweir     const OUString& EventMethod
712cdf0e10cSrcweir )
713cdf0e10cSrcweir     throw( IllegalArgumentException, ServiceNotRegisteredException, CannotCreateAdapterException, IntrospectionException, RuntimeException )
714cdf0e10cSrcweir {
715cdf0e10cSrcweir     // FilterListener anmelden
716cdf0e10cSrcweir     Reference< XAllListener > aFilterListener = (XAllListener*)
717cdf0e10cSrcweir         new FilterAllListenerImpl( this, EventMethod, AllListener );
718cdf0e10cSrcweir     return attachListener( xObject, aFilterListener, Helper, ListenerType, AddListenerParam);
719cdf0e10cSrcweir }
720cdf0e10cSrcweir 
721cdf0e10cSrcweir // XEventAttacher
removeListener(const Reference<XInterface> & xObject,const OUString & ListenerType,const OUString & AddListenerParam,const Reference<XEventListener> & aToRemoveListener)722cdf0e10cSrcweir void EventAttacherImpl::removeListener
723cdf0e10cSrcweir (
724cdf0e10cSrcweir     const Reference< XInterface >& xObject,
725cdf0e10cSrcweir     const OUString& ListenerType,
726cdf0e10cSrcweir     const OUString& AddListenerParam,
727cdf0e10cSrcweir     const Reference< XEventListener >& aToRemoveListener
728cdf0e10cSrcweir )
729cdf0e10cSrcweir     throw( IllegalArgumentException, IntrospectionException, RuntimeException )
730cdf0e10cSrcweir {
731cdf0e10cSrcweir     if( !xObject.is() || !aToRemoveListener.is() )
732cdf0e10cSrcweir         throw IllegalArgumentException();
733cdf0e10cSrcweir 
734cdf0e10cSrcweir     // Listener-Klasse per Reflection besorgen
735cdf0e10cSrcweir     Reference< XIdlReflection > xReflection = getReflection();
736cdf0e10cSrcweir     if( !xReflection.is() )
737cdf0e10cSrcweir         throw IntrospectionException();
738cdf0e10cSrcweir 
739cdf0e10cSrcweir     // Abmelden, dazu passende removeListener-Methode aufrufen
740cdf0e10cSrcweir     // Zunaechst ueber Introspection gehen, da die Methoden in der gleichen
741cdf0e10cSrcweir     // Weise analysiert werden koennen. Fuer bessere Performance entweder
742cdf0e10cSrcweir     // hier nochmal implementieren oder die Impl-Methode der Introspection
743cdf0e10cSrcweir     // fuer diesen Zweck konfigurierbar machen.
744cdf0e10cSrcweir 
745cdf0e10cSrcweir     // Introspection-Service holen
746cdf0e10cSrcweir     Reference< XIntrospection > xIntrospection = getIntrospection();
747cdf0e10cSrcweir     if( !xIntrospection.is() )
748cdf0e10cSrcweir         throw IntrospectionException();
749cdf0e10cSrcweir 
750cdf0e10cSrcweir     // und inspecten
751cdf0e10cSrcweir     Any aObjAny( &xObject, ::getCppuType( (const Reference< XInterface > *)0) );
752cdf0e10cSrcweir     Reference< XIntrospectionAccess > xAccess = xIntrospection->inspect( aObjAny );
753cdf0e10cSrcweir     if( !xAccess.is() )
754cdf0e10cSrcweir         throw IntrospectionException();
755cdf0e10cSrcweir 
756cdf0e10cSrcweir     // Name der removeListener-Methode zusammenbasteln
757cdf0e10cSrcweir     OUString aRemoveListenerName;
758cdf0e10cSrcweir     OUString aListenerName( ListenerType );
759cdf0e10cSrcweir     sal_Int32 nIndex = aListenerName.lastIndexOf( '.' );
760cdf0e10cSrcweir     // set index to the interface name without package name
761cdf0e10cSrcweir     if( nIndex == -1 )
762cdf0e10cSrcweir         // not found
763cdf0e10cSrcweir         nIndex = 0;
764cdf0e10cSrcweir     else
765cdf0e10cSrcweir         nIndex++;
766cdf0e10cSrcweir     if( aListenerName[nIndex] == 'X' )
767cdf0e10cSrcweir         // erase X from the interface name
768cdf0e10cSrcweir         aListenerName = aListenerName.copy( nIndex +1 );
769cdf0e10cSrcweir     aRemoveListenerName = OUString( RTL_CONSTASCII_USTRINGPARAM("remove") ) + aListenerName;
770cdf0e10cSrcweir 
771cdf0e10cSrcweir     // Methoden nach der passenden addListener-Methode durchsuchen
772cdf0e10cSrcweir     Sequence< Reference< XIdlMethod > > aMethodSeq = xAccess->getMethods( MethodConcept::LISTENER );
773cdf0e10cSrcweir     sal_uInt32 i, nLen = aMethodSeq.getLength();
774cdf0e10cSrcweir     const Reference< XIdlMethod >* pMethods = aMethodSeq.getConstArray();
775cdf0e10cSrcweir     for( i = 0 ; i < nLen ; i++ )
776cdf0e10cSrcweir     {
777cdf0e10cSrcweir         // Methode ansprechen
778cdf0e10cSrcweir         const Reference< XIdlMethod >& rxMethod = pMethods[i];
779cdf0e10cSrcweir 
780cdf0e10cSrcweir         // Ist es die richtige Methode?
781cdf0e10cSrcweir         if( aRemoveListenerName == rxMethod->getName() )
782cdf0e10cSrcweir         {
783cdf0e10cSrcweir             Sequence< Reference< XIdlClass > > params = rxMethod->getParameterTypes();
784cdf0e10cSrcweir             sal_uInt32 nParamCount = params.getLength();
785cdf0e10cSrcweir 
786cdf0e10cSrcweir             // Nur der Listener als Parameter?
787cdf0e10cSrcweir             if( nParamCount == 1 )
788cdf0e10cSrcweir             {
789cdf0e10cSrcweir                 Sequence< Any > args( 1 );
790cdf0e10cSrcweir                 args.getArray()[0] <<= aToRemoveListener;
791cdf0e10cSrcweir                 try
792cdf0e10cSrcweir                 {
793cdf0e10cSrcweir                     rxMethod->invoke( aObjAny, args );
794cdf0e10cSrcweir                 }
795cdf0e10cSrcweir                 catch( InvocationTargetException& )
796cdf0e10cSrcweir                 {
797cdf0e10cSrcweir                     throw IntrospectionException();
798cdf0e10cSrcweir                 }
799cdf0e10cSrcweir             }
800cdf0e10cSrcweir             // Sonst den Zusatzparameter mit uebergeben
801cdf0e10cSrcweir             else if( nParamCount == 2 )
802cdf0e10cSrcweir             {
803cdf0e10cSrcweir                 Sequence< Any > args( 2 );
804cdf0e10cSrcweir                 Any* pAnys = args.getArray();
805cdf0e10cSrcweir 
806cdf0e10cSrcweir                 // Typ des 1. Parameters pruefen
807cdf0e10cSrcweir                 Reference< XIdlClass > xParamClass = params.getConstArray()[0];
808cdf0e10cSrcweir                 if( xParamClass->getTypeClass() == TypeClass_STRING )
809cdf0e10cSrcweir                     pAnys[0] <<= AddListenerParam;
810cdf0e10cSrcweir 
811cdf0e10cSrcweir                 // 2. Parameter == Listener? TODO: Pruefen!
812cdf0e10cSrcweir                 pAnys[1] <<= aToRemoveListener;
813cdf0e10cSrcweir 
814cdf0e10cSrcweir                 // TODO: Konvertierung String -> ?
815cdf0e10cSrcweir                 // else
816cdf0e10cSrcweir                 try
817cdf0e10cSrcweir                 {
818cdf0e10cSrcweir                     rxMethod->invoke( aObjAny, args );
819cdf0e10cSrcweir                 }
820cdf0e10cSrcweir                 catch( InvocationTargetException& )
821cdf0e10cSrcweir                 {
822cdf0e10cSrcweir                     throw IntrospectionException();
823cdf0e10cSrcweir                 }
824cdf0e10cSrcweir             }
825cdf0e10cSrcweir             break;
826cdf0e10cSrcweir         }
827cdf0e10cSrcweir     }
828cdf0e10cSrcweir }
829cdf0e10cSrcweir 
830cdf0e10cSrcweir }
831cdf0e10cSrcweir 
832cdf0e10cSrcweir extern "C"
833cdf0e10cSrcweir {
834cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)835*0b5197c8Sdamjan SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
836cdf0e10cSrcweir     const sal_Char ** ppEnvTypeName, uno_Environment ** )
837cdf0e10cSrcweir {
838cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
839cdf0e10cSrcweir }
840cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)841*0b5197c8Sdamjan SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
842cdf0e10cSrcweir     const sal_Char * pImplName, void * pServiceManager, void * )
843cdf0e10cSrcweir {
844cdf0e10cSrcweir     void * pRet = 0;
845cdf0e10cSrcweir 
846cdf0e10cSrcweir     if (pServiceManager && rtl_str_compare( pImplName, IMPLNAME ) == 0)
847cdf0e10cSrcweir     {
848cdf0e10cSrcweir         Reference< XSingleServiceFactory > xFactory( createOneInstanceFactory(
849cdf0e10cSrcweir             reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
850cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLNAME ) ),
851cdf0e10cSrcweir             ::comp_EventAttacher::EventAttacherImpl_CreateInstance,
852cdf0e10cSrcweir             ::comp_EventAttacher::EventAttacherImpl::getSupportedServiceNames_Static() ) );
853cdf0e10cSrcweir 
854cdf0e10cSrcweir         if (xFactory.is())
855cdf0e10cSrcweir         {
856cdf0e10cSrcweir             xFactory->acquire();
857cdf0e10cSrcweir             pRet = xFactory.get();
858cdf0e10cSrcweir         }
859cdf0e10cSrcweir     }
860cdf0e10cSrcweir 
861cdf0e10cSrcweir     return pRet;
862cdf0e10cSrcweir }
863cdf0e10cSrcweir }
864