xref: /aoo4110/main/sfx2/source/inc/eventsupplier.hxx (revision b1cdbd2c)
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 #ifndef _SFX_EVENTSUPPLIER_HXX_
25 #define _SFX_EVENTSUPPLIER_HXX_
26 
27 #include <com/sun/star/lang/XTypeProvider.hpp>
28 #include <com/sun/star/container/XNameReplace.hpp>
29 #include <com/sun/star/container/XSet.hpp>
30 #include <com/sun/star/document/XEventListener.hpp>
31 #include <com/sun/star/document/XEventBroadcaster.hpp>
32 #include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
33 #include <com/sun/star/document/XDocumentEventListener.hpp>
34 #include <com/sun/star/document/XEventsSupplier.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/task/XJobExecutor.hpp>
37 #include <com/sun/star/uno/Reference.hxx>
38 #include <com/sun/star/uno/Type.hxx>
39 #include <sal/types.h>
40 #include <osl/mutex.hxx>
41 #include <cppuhelper/weak.hxx>
42 #include <cppuhelper/implbase1.hxx>
43 #include <cppuhelper/implbase2.hxx>
44 #include <cppuhelper/implbase3.hxx>
45 #include <cppuhelper/implbase4.hxx>
46 #include <cppuhelper/implbase7.hxx>
47 #include <comphelper/sequenceashashmap.hxx>
48 #include <comphelper/sequenceasvector.hxx>
49 #include <sfx2/sfxuno.hxx>
50 
51 #include <vos/mutex.hxx>
52 #include <cppuhelper/interfacecontainer.hxx>
53 #include <svl/lstner.hxx>
54 #include <unotools/eventcfg.hxx>
55 
56 namespace comphelper
57 {
58     class NamedValueCollection;
59 }
60 
61 //--------------------------------------------------------------------------------------------------------
62 
63 #define NOSUCHELEMENTEXCEPTION		::com::sun::star::container::NoSuchElementException
64 #define XNAMEREPLACE				::com::sun::star::container::XNameReplace
65 #define DOCEVENTOBJECT				::com::sun::star::document::EventObject
66 #define	XEVENTBROADCASTER			::com::sun::star::document::XEventBroadcaster
67 #define XDOCEVENTLISTENER			::com::sun::star::document::XEventListener
68 #define	XEVENTSSUPPLIER				::com::sun::star::document::XEventsSupplier
69 #define XJOBEXECUTOR                ::com::sun::star::task::XJobExecutor
70 #define EVENTOBJECT					::com::sun::star::lang::EventObject
71 #define ILLEGALARGUMENTEXCEPTION	::com::sun::star::lang::IllegalArgumentException
72 #define WRAPPEDTARGETEXCEPTION		::com::sun::star::lang::WrappedTargetException
73 #define ANY							::com::sun::star::uno::Any
74 #define	REFERENCE					::com::sun::star::uno::Reference
75 #define WEAKREFERENCE               ::com::sun::star::uno::WeakReference
76 #define RUNTIMEEXCEPTION			::com::sun::star::uno::RuntimeException
77 #define SEQUENCE					::com::sun::star::uno::Sequence
78 #define UNOTYPE						::com::sun::star::uno::Type
79 #define OUSTRING					::rtl::OUString
80 #define	OINTERFACECONTAINERHELPER   ::cppu::OInterfaceContainerHelper
81 
82 //--------------------------------------------------------------------------------------------------------
83 
84 class SfxObjectShell;
85 class SfxBaseModel;
86 class SvxMacro;
87 
88 //--------------------------------------------------------------------------------------------------------
89 
90 class SfxEvents_Impl : public ::cppu::WeakImplHelper2< ::com::sun::star::container::XNameReplace, ::com::sun::star::document::XEventListener  >
91 {
92 	SEQUENCE< OUSTRING >			maEventNames;
93 	SEQUENCE< ANY >					maEventData;
94 	REFERENCE< XEVENTBROADCASTER >	mxBroadcaster;
95 	::osl::Mutex					maMutex;
96 	SfxObjectShell				   *mpObjShell;
97 
98     sal_Bool                    Warn_Impl( const String& );
99 
100 public:
101 								SfxEvents_Impl( SfxObjectShell* pShell,
102 												REFERENCE< XEVENTBROADCASTER > xBroadcaster );
103 							   ~SfxEvents_Impl();
104 
105 	//  --- XNameReplace ---
106     virtual void SAL_CALL		replaceByName( const OUSTRING & aName, const ANY & aElement )
107 									throw( ILLEGALARGUMENTEXCEPTION, NOSUCHELEMENTEXCEPTION,
108 										   WRAPPEDTARGETEXCEPTION, RUNTIMEEXCEPTION );
109 
110 	//  --- XNameAccess ( parent of XNameReplace ) ---
111     virtual ANY SAL_CALL		getByName( const OUSTRING& aName )
112 									throw( NOSUCHELEMENTEXCEPTION, WRAPPEDTARGETEXCEPTION,
113 										   RUNTIMEEXCEPTION );
114     virtual SEQUENCE< OUSTRING > SAL_CALL getElementNames() throw ( RUNTIMEEXCEPTION );
115     virtual sal_Bool SAL_CALL	hasByName( const OUSTRING& aName ) throw ( RUNTIMEEXCEPTION );
116 
117 	//  --- XElementAccess ( parent of XNameAccess ) ---
118     virtual UNOTYPE SAL_CALL	getElementType() throw ( RUNTIMEEXCEPTION );
119     virtual sal_Bool SAL_CALL	hasElements() throw ( RUNTIMEEXCEPTION );
120 
121     // --- ::document::XEventListener ---
122     virtual void SAL_CALL		notifyEvent( const DOCEVENTOBJECT& aEvent )
123 									throw( RUNTIMEEXCEPTION );
124 
125     // --- ::lang::XEventListener ---
126     virtual void SAL_CALL		disposing( const EVENTOBJECT& Source )
127 									throw( RUNTIMEEXCEPTION );
128 
129     static SvxMacro*            ConvertToMacro( const ANY& rElement, SfxObjectShell* pDoc, sal_Bool bNormalizeMacro );
130 	static void					NormalizeMacro( const ANY& rIn, ANY& rOut, SfxObjectShell* pDoc );
131     static void					NormalizeMacro(
132                                     const ::comphelper::NamedValueCollection& i_eventDescriptor,
133                                     ::comphelper::NamedValueCollection& o_normalizedDescriptor,
134                                     SfxObjectShell* i_document );
135 };
136 
137 //=============================================================================
138 struct ModelCollectionMutexBase
139 {
140     public:
141         ::osl::Mutex m_aLock;
142 };
143 
144 //=============================================================================
145 typedef ::std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel > > TModelList;
146 
147 //=============================================================================
148 class ModelCollectionEnumeration : public ModelCollectionMutexBase
149                                  , public ::cppu::WeakImplHelper1< ::com::sun::star::container::XEnumeration >
150 {
151 
152     //-------------------------------------------------------------------------
153     // member
154     //-------------------------------------------------------------------------
155     private:
156         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xSMGR;
157         TModelList m_lModels;
158         TModelList::iterator m_pEnumerationIt;
159 
160     //-------------------------------------------------------------------------
161     // native interface
162     //-------------------------------------------------------------------------
163     public:
164         ModelCollectionEnumeration(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xSMGR);
165         virtual ~ModelCollectionEnumeration();
166         void setModelList(const TModelList& rList);
167 
168     //-------------------------------------------------------------------------
169     // uno interface
170     //-------------------------------------------------------------------------
171     public:
172 
173         // css.container.XEnumeration
174         virtual sal_Bool SAL_CALL hasMoreElements()
175             throw(::com::sun::star::uno::RuntimeException);
176 
177         virtual ::com::sun::star::uno::Any SAL_CALL nextElement()
178             throw(::com::sun::star::container::NoSuchElementException,
179                   ::com::sun::star::lang::WrappedTargetException     ,
180                   ::com::sun::star::uno::RuntimeException            );
181 };
182 
183 //=============================================================================
184 class SfxGlobalEvents_Impl : public ModelCollectionMutexBase
185                            , public ::cppu::WeakImplHelper7< ::com::sun::star::lang::XServiceInfo
186                                                            , ::com::sun::star::document::XEventsSupplier
187                                                            , ::com::sun::star::document::XEventBroadcaster
188                                                            , ::com::sun::star::document::XDocumentEventBroadcaster
189                                                            , ::com::sun::star::document::XEventListener
190                                                            , ::com::sun::star::document::XDocumentEventListener
191                                                            , ::com::sun::star::container::XSet >
192 {
193 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xSMGR;
194 	::com::sun::star::uno::Reference< ::com::sun::star::container::XNameReplace > m_xEvents;
195     ::com::sun::star::uno::WeakReference< ::com::sun::star::document::XEventListener > m_xJobExecutorListener;
196 	OINTERFACECONTAINERHELPER m_aLegacyListeners;
197     OINTERFACECONTAINERHELPER m_aDocumentListeners;
198     TModelList m_lModels;
199 	GlobalEventConfig* pImp;
200 
201 public:
202     SfxGlobalEvents_Impl(const com::sun::star::uno::Reference < ::com::sun::star::lang::XMultiServiceFactory >& xSMGR);
203     virtual ~SfxGlobalEvents_Impl();
204 
205     SFX_DECL_XSERVICEINFO
206 
207     // css.document.XEventBroadcaster
208     virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameReplace > SAL_CALL getEvents()
209         throw(::com::sun::star::uno::RuntimeException);
210 
211     virtual void SAL_CALL addEventListener(const ::com::sun::star::uno::Reference< ::com::sun::star::document::XEventListener >& xListener)
212         throw(::com::sun::star::uno::RuntimeException);
213 
214     virtual void SAL_CALL removeEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::document::XEventListener >& xListener)
215         throw(::com::sun::star::uno::RuntimeException);
216 
217     // css.document.XDocumentEventBroadcaster
218     virtual void SAL_CALL addDocumentEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::document::XDocumentEventListener >& _Listener ) throw (::com::sun::star::uno::RuntimeException);
219     virtual void SAL_CALL removeDocumentEventListener( const ::com::sun::star::uno::Reference< ::com::sun::star::document::XDocumentEventListener >& _Listener ) throw (::com::sun::star::uno::RuntimeException);
220     virtual void SAL_CALL notifyDocumentEvent( const ::rtl::OUString& _EventName, const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XController2 >& _ViewController, const ::com::sun::star::uno::Any& _Supplement ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
221 
222     // css.document.XEventListener
223     virtual void SAL_CALL notifyEvent(const ::com::sun::star::document::EventObject& aEvent)
224         throw(::com::sun::star::uno::RuntimeException);
225 
226     // css.document.XDocumentEventListener
227     virtual void SAL_CALL documentEventOccured( const ::com::sun::star::document::DocumentEvent& Event ) throw (::com::sun::star::uno::RuntimeException);
228 
229     // css.container.XSet
230     virtual sal_Bool SAL_CALL has(const ::com::sun::star::uno::Any& aElement)
231         throw(::com::sun::star::uno::RuntimeException);
232 
233     virtual void SAL_CALL insert(const ::com::sun::star::uno::Any& aElement)
234         throw(::com::sun::star::lang::IllegalArgumentException  ,
235               ::com::sun::star::container::ElementExistException,
236               ::com::sun::star::uno::RuntimeException           );
237 
238     virtual void SAL_CALL remove(const ::com::sun::star::uno::Any& aElement)
239         throw(::com::sun::star::lang::IllegalArgumentException   ,
240               ::com::sun::star::container::NoSuchElementException,
241               ::com::sun::star::uno::RuntimeException            );
242 
243     // css.container.XEnumerationAccess
244     virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createEnumeration()
245         throw(::com::sun::star::uno::RuntimeException);
246 
247     // css.container.XElementAccess
248     virtual ::com::sun::star::uno::Type SAL_CALL getElementType()
249         throw(::com::sun::star::uno::RuntimeException);
250 
251     virtual sal_Bool SAL_CALL hasElements()
252         throw(::com::sun::star::uno::RuntimeException);
253 
254     // css.lang.XEventListener
255     virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& aEvent)
256         throw(::com::sun::star::uno::RuntimeException);
257 
258 private:
259 
260     // threadsafe
261     void implts_notifyJobExecution(const ::com::sun::star::document::EventObject& aEvent);
262     void implts_checkAndExecuteEventBindings(const ::com::sun::star::document::DocumentEvent& aEvent);
263     void implts_notifyListener(const ::com::sun::star::document::DocumentEvent& aEvent);
264 
265     // not threadsafe
266     TModelList::iterator impl_searchDoc(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& xModel);
267 };
268 
269 #endif
270