merge the formfield patch from ooo-build
[ooovba.git] / framework / inc / dispatch / basedispatcher.hxx
blobf74efb05ca8e7963299e069364beddd65add9ed8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: basedispatcher.hxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_DISPATCH_BASEDISPATCHER_HXX_
32 #define __FRAMEWORK_DISPATCH_BASEDISPATCHER_HXX_
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
38 #include <classes/taskcreator.hxx>
39 #include <threadhelp/resetableguard.hxx>
40 #include <threadhelp/threadhelpbase.hxx>
42 #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONBASE_HXX_
43 #include <threadhelp/transactionbase.hxx>
44 #endif
45 #include <macros/xinterface.hxx>
46 #include <macros/xtypeprovider.hxx>
47 #include <macros/debug.hxx>
48 #include <macros/generic.hxx>
49 #include <stdtypes.h>
51 //_________________________________________________________________________________________________________________
52 // interface includes
53 //_________________________________________________________________________________________________________________
54 #include <com/sun/star/lang/XTypeProvider.hpp>
55 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
56 #include <com/sun/star/util/URL.hpp>
57 #include <com/sun/star/frame/DispatchDescriptor.hpp>
58 #include <com/sun/star/beans/PropertyValue.hpp>
59 #include <com/sun/star/frame/XDispatchResultListener.hpp>
60 #include <com/sun/star/frame/XFrameLoader.hpp>
61 #include <com/sun/star/frame/XLoadEventListener.hpp>
62 #include <com/sun/star/frame/XDesktop.hpp>
63 #include <com/sun/star/frame/FeatureStateEvent.hpp>
64 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
66 //_________________________________________________________________________________________________________________
67 // other includes
68 //_________________________________________________________________________________________________________________
69 #include <cppuhelper/weak.hxx>
70 #include <cppuhelper/weakref.hxx>
71 #include <cppuhelper/interfacecontainer.h>
72 /*DRAFT
73 #include <svtools/historyoptions.hxx>
76 //_________________________________________________________________________________________________________________
77 // namespace
78 //_________________________________________________________________________________________________________________
80 namespace framework{
82 //_________________________________________________________________________________________________________________
83 // exported const
84 //_________________________________________________________________________________________________________________
86 //_________________________________________________________________________________________________________________
87 // exported definitions
88 //_________________________________________________________________________________________________________________
90 /*-************************************************************************************************************//**
91 @descr We must support loading of different URLs with different handler or loader into different tasks simultaniously.
92 They call us back to return state of operation. We need some informations to distinguish
93 between these different "loading threads".
94 This is the reason to implement this dynamicly list.
96 @attention I maked class LoaderThreads threadsafe! Using will be easier in a multithreaded environment.
97 struct DispatchBinding doesn't need that!
98 *//*-*************************************************************************************************************/
99 struct LoadBinding
101 //-------------------------------------------------------------------------------------------------------------
102 public:
104 //---------------------------------------------------------------------------------------------------------
105 inline LoadBinding()
107 free();
110 //---------------------------------------------------------------------------------------------------------
111 // use to initialize struct for asynchronous dispatching by using handler
112 inline LoadBinding( const css::util::URL& aNewURL ,
113 const css::uno::Sequence< css::beans::PropertyValue > lNewDescriptor ,
114 const css::uno::Reference< css::frame::XDispatch >& xNewHandler ,
115 const css::uno::Any& aNewAsyncInfo )
117 free();
118 xHandler = xNewHandler ;
119 aURL = aNewURL ;
120 lDescriptor = lNewDescriptor;
121 aAsyncInfo = aNewAsyncInfo ;
124 //---------------------------------------------------------------------------------------------------------
125 // use to initialize struct for asynchronous loading by using frame loader
126 inline LoadBinding( const css::util::URL& aNewURL ,
127 const css::uno::Sequence< css::beans::PropertyValue > lNewDescriptor ,
128 const css::uno::Reference< css::frame::XFrame >& xNewFrame ,
129 const css::uno::Reference< css::frame::XFrameLoader >& xNewLoader ,
130 const css::uno::Any& aNewAsyncInfo )
132 free();
133 xLoader = xNewLoader ;
134 xFrame = xNewFrame ;
135 aURL = aNewURL ;
136 lDescriptor = lNewDescriptor;
137 aAsyncInfo = aNewAsyncInfo ;
140 //---------------------------------------------------------------------------------------------------------
141 // dont forget toe release used references
142 inline ~LoadBinding()
144 free();
147 //---------------------------------------------------------------------------------------------------------
148 inline void free()
150 xHandler = css::uno::Reference< css::frame::XDispatch >() ;
151 xLoader = css::uno::Reference< css::frame::XFrameLoader >();
152 xFrame = css::uno::Reference< css::frame::XFrame >() ;
153 aURL = css::util::URL() ;
154 lDescriptor = css::uno::Sequence< css::beans::PropertyValue >();
155 aAsyncInfo = css::uno::Any() ;
158 //-------------------------------------------------------------------------------------------------------------
159 public:
160 css::uno::Reference< css::frame::XDispatch > xHandler ; // if handler was used, this reference will be valid
161 css::uno::Reference< css::frame::XFrameLoader > xLoader ; // if loader was used, this reference will be valid
162 css::uno::Reference< css::frame::XFrame > xFrame ; // Target of loading
163 css::util::URL aURL ; // dispatched URL - neccessary to find listener for status event!
164 css::uno::Sequence< css::beans::PropertyValue > lDescriptor ; // dispatched arguments - neccessary for "reactForLoadingState()"!
165 css::uno::Any aAsyncInfo ; // superclasses could use them to save her own user specific data for these asynchron call-info
166 css::uno::Reference< css::frame::XDispatchResultListener > xListener;
169 //*****************************************************************************************************************
170 class LoaderThreads : private ::std::vector< LoadBinding >
171 , private ThreadHelpBase
173 //-------------------------------------------------------------------------------------------------------------
174 public:
176 //---------------------------------------------------------------------------------------------------------
177 inline LoaderThreads()
178 : ThreadHelpBase()
182 //---------------------------------------------------------------------------------------------------------
183 inline void append( const LoadBinding& aBinding )
185 ResetableGuard aGuard( m_aLock );
186 push_back( aBinding );
189 //---------------------------------------------------------------------------------------------------------
190 /// search for handler thread in list wich match given parameter and delete it
191 inline sal_Bool searchAndForget( const css::uno::Reference < css::frame::XDispatchResultListener >& rListener, LoadBinding& aBinding )
193 ResetableGuard aGuard( m_aLock );
194 sal_Bool bFound = sal_False;
195 for( iterator pItem=begin(); pItem!=end(); ++pItem )
197 if( pItem->xListener == rListener )
199 aBinding = *pItem;
200 erase( pItem );
201 bFound = sal_True;
202 break;
205 return bFound;
208 //---------------------------------------------------------------------------------------------------------
209 /// search for loader thread in list wich match given parameter and delete it
210 inline sal_Bool searchAndForget( const css::uno::Reference< css::frame::XFrameLoader > xLoader, LoadBinding& aBinding )
212 ResetableGuard aGuard( m_aLock );
213 sal_Bool bFound = sal_False;
214 for( iterator pItem=begin(); pItem!=end(); ++pItem )
216 if( pItem->xLoader == xLoader )
218 aBinding = *pItem;
219 erase( pItem );
220 bFound = sal_True;
221 break;
224 return bFound;
227 //---------------------------------------------------------------------------------------------------------
228 // free ALL memory ... I hope it
229 inline void free()
231 ResetableGuard aGuard( m_aLock );
232 LoaderThreads().swap( *this );
236 /*-************************************************************************************************************//**
237 @short base class for dispatcher implementations
238 @descr Most of our dispatch implementations do everytime the same. They try to handle or load
239 somethinmg into a target ... normaly a frame/task/pluginframe!
240 They must do it synchron or sometimes asynchron. They must wait for callbacks and
241 notify registered listener with right status events.
242 All these things are implemented by this baseclass. You should override some methods
243 to change something.
245 "dispatch()" => should be you dispatch algorithm
246 "reactForLoadingState()" => do something depending from loading state ...
248 @implements XInterface
249 XDispatch
250 XLoadEventListener
251 XEventListener
253 @base ThreadHelpBase
254 TransactionBase
255 OWeakObject
257 @devstatus ready to use
258 @threadsafe yes
259 *//*-*************************************************************************************************************/
260 class BaseDispatcher : // interfaces
261 public css::lang::XTypeProvider ,
262 public css::frame::XNotifyingDispatch ,
263 public css::frame::XLoadEventListener , // => XEventListener too!
264 // baseclasses
265 // Order is neccessary for right initialization!
266 protected ThreadHelpBase ,
267 protected TransactionBase ,
268 public ::cppu::OWeakObject
270 //-------------------------------------------------------------------------------------------------------------
271 // public methods
272 //-------------------------------------------------------------------------------------------------------------
273 public:
275 // constructor / destructor
276 BaseDispatcher( const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory ,
277 const css::uno::Reference< css::frame::XFrame >& xOwnerFrame );
279 void dispatchFinished ( const css::frame::DispatchResultEvent& aEvent, const css::uno::Reference < css::frame::XDispatchResultListener >& rListener );
281 // XInterface
282 DECLARE_XINTERFACE
283 DECLARE_XTYPEPROVIDER
285 // XNotifyingDispatch
286 virtual void SAL_CALL dispatchWithNotification ( const css::util::URL& aURL,
287 const css::uno::Sequence< css::beans::PropertyValue >& aArgs,
288 const css::uno::Reference< css::frame::XDispatchResultListener >& Listener ) throw ( css::uno::RuntimeException);
290 // XDispatch
291 virtual void SAL_CALL dispatch ( const css::util::URL& aURL ,
292 const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException ) = 0;
293 virtual void SAL_CALL addStatusListener ( const css::uno::Reference< css::frame::XStatusListener >& xListener ,
294 const css::util::URL& aURL ) throw( css::uno::RuntimeException );
295 virtual void SAL_CALL removeStatusListener ( const css::uno::Reference< css::frame::XStatusListener >& xListener ,
296 const css::util::URL& aURL ) throw( css::uno::RuntimeException );
298 // XLoadEventListener
299 virtual void SAL_CALL loadFinished ( const css::uno::Reference< css::frame::XFrameLoader >& xLoader ) throw( css::uno::RuntimeException );
300 virtual void SAL_CALL loadCancelled ( const css::uno::Reference< css::frame::XFrameLoader >& xLoader ) throw( css::uno::RuntimeException );
302 // XEventListener
303 virtual void SAL_CALL disposing ( const css::lang::EventObject& aEvent ) throw( css::uno::RuntimeException );
305 //-------------------------------------------------------------------------------------------------------------
306 // protected methods
307 //-------------------------------------------------------------------------------------------------------------
308 protected:
309 virtual ~BaseDispatcher();
311 /*-****************************************************************************************************//**
312 @short you should react for successfully or failed load/handle operations.
313 @descr These baseclass implement handling of dispatched URLs and synchronous/asynchronous loading
314 of it into a target frame. It implement the complete listener mechanism to get events from
315 used loader or handler and sending of status events to registered listener too!
316 But we couldn't react for this events in all cases.
317 May be - you wish to reactivate suspended controllers or wish to delete a new created
318 task if operation failed ...!?
319 By overwriting these pure virtual methods it's possible to do such things.
320 We call you with all available informations ... you should react for it.
321 BUT - don't send any status events to your listener! We will do it everytime.
322 (other listener could be informed as well!)
324 You will called back in: a) "reactForLoadingState()" , if URL was loaded into a frame
325 b) "reactForHandlingState()", if URL was handled by a registered content handler
326 (without using a target frame!)
328 @seealso method statusChanged()
329 @seealso method loadFinished()
330 @seealso method loadCancelled()
332 @param "aURL" , original dispatched URL
333 @param "lDescriptor" , original dispatched arguments
334 @param "xTarget" , target of operation (could be NULL if URL was handled not loaded!)
335 @param "bState" , state of operation
336 @return -
338 @onerror -
339 @threadsafe -
340 *//*-*****************************************************************************************************/
341 virtual void SAL_CALL reactForLoadingState ( const css::util::URL& aURL ,
342 const css::uno::Sequence< css::beans::PropertyValue >& lDescriptor ,
343 const css::uno::Reference< css::frame::XFrame >& xTarget ,
344 sal_Bool bState ,
345 const css::uno::Any& aAsyncInfo ) = 0;
347 virtual void SAL_CALL reactForHandlingState( const css::util::URL& aURL ,
348 const css::uno::Sequence< css::beans::PropertyValue >& lDescriptor ,
349 sal_Bool bState ,
350 const css::uno::Any& aAsyncInfo ) = 0;
352 //-------------------------------------------------------------------------------------------------------------
353 // protected methods
354 //-------------------------------------------------------------------------------------------------------------
355 protected:
356 ::rtl::OUString implts_detectType ( const css::util::URL& aURL ,
357 css::uno::Sequence< css::beans::PropertyValue >& lDescriptor ,
358 sal_Bool bDeep );
359 sal_Bool implts_handleIt ( const css::util::URL& aURL ,
360 css::uno::Sequence< css::beans::PropertyValue >& lDescriptor ,
361 const ::rtl::OUString& sTypeName ,
362 const css::uno::Any& aAsyncInfo = css::uno::Any() );
363 sal_Bool implts_loadIt ( const css::util::URL& aURL ,
364 css::uno::Sequence< css::beans::PropertyValue >& lDescriptor ,
365 const ::rtl::OUString& sTypeName ,
366 const css::uno::Reference< css::frame::XFrame >& xTarget ,
367 const css::uno::Any& aAsyncInfo = css::uno::Any() );
368 void implts_enableFrame ( const css::uno::Reference< css::frame::XFrame >& xFrame ,
369 const css::uno::Sequence< css::beans::PropertyValue >& lDescriptor );
370 void implts_disableFrame ( const css::uno::Reference< css::frame::XFrame >& xFrame );
371 sal_Bool implts_deactivateController ( const css::uno::Reference< css::frame::XController >& xController );
372 sal_Bool implts_reactivateController ( const css::uno::Reference< css::frame::XController >& xController );
373 void implts_sendResultEvent ( const css::uno::Reference< css::frame::XFrame >& xEventSource ,
374 const ::rtl::OUString& sURL ,
375 sal_Bool bLoadState );
377 //-------------------------------------------------------------------------------------------------------------
378 // variables
379 // - should be private normaly ...
380 // - but some super classes need access to some of them => protected!
381 //-------------------------------------------------------------------------------------------------------------
382 protected:
383 css::uno::Reference< css::lang::XMultiServiceFactory > m_xFactory ; /// global uno service manager to create new services
384 css::uno::WeakReference< css::frame::XFrame > m_xOwner ; /// weakreference to owner (Don't use a hard reference. Owner can't delete us then!)
386 private:
387 LoaderThreads m_aLoaderThreads ; /// list of bindings between handler/loader, tasks and loaded URLs
388 ListenerHash m_aListenerContainer ; /// hash table for listener at specified URLs
390 }; // class BaseDispatcher
392 } // namespace framework
394 #endif // #ifndef __FRAMEWORK_DISPATCH_BASEDISPATCHER_HXX_