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