1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 #ifndef __FRAMEWORK_DISPATCH_BASEDISPATCHER_HXX_
29 #define __FRAMEWORK_DISPATCH_BASEDISPATCHER_HXX_
31 //_________________________________________________________________________________________________________________
33 //_________________________________________________________________________________________________________________
35 #include <classes/taskcreator.hxx>
36 #include <threadhelp/resetableguard.hxx>
37 #include <threadhelp/threadhelpbase.hxx>
39 #ifndef __FRAMEWORK_THREADHELP_TRANSACTIONBASE_HXX_
40 #include <threadhelp/transactionbase.hxx>
42 #include <macros/xinterface.hxx>
43 #include <macros/xtypeprovider.hxx>
44 #include <macros/debug.hxx>
45 #include <macros/generic.hxx>
48 //_________________________________________________________________________________________________________________
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>
63 //_________________________________________________________________________________________________________________
65 //_________________________________________________________________________________________________________________
66 #include <cppuhelper/weak.hxx>
67 #include <cppuhelper/weakref.hxx>
68 #include <cppuhelper/interfacecontainer.h>
70 #include <unotools/historyoptions.hxx>
73 //_________________________________________________________________________________________________________________
75 //_________________________________________________________________________________________________________________
79 //_________________________________________________________________________________________________________________
81 //_________________________________________________________________________________________________________________
83 //_________________________________________________________________________________________________________________
84 // exported definitions
85 //_________________________________________________________________________________________________________________
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.
93 @attention I maked class LoaderThreads threadsafe! Using will be easier in a multithreaded environment.
94 struct DispatchBinding doesn't need that!
95 *//*-*************************************************************************************************************/
98 //-------------------------------------------------------------------------------------------------------------
101 //---------------------------------------------------------------------------------------------------------
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
)
115 xHandler
= xNewHandler
;
117 lDescriptor
= lNewDescriptor
;
118 aAsyncInfo
= aNewAsyncInfo
;
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
)
130 xLoader
= xNewLoader
;
133 lDescriptor
= lNewDescriptor
;
134 aAsyncInfo
= aNewAsyncInfo
;
137 //---------------------------------------------------------------------------------------------------------
138 // dont forget toe release used references
139 inline ~LoadBinding()
144 //---------------------------------------------------------------------------------------------------------
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() ;
155 //-------------------------------------------------------------------------------------------------------------
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
;
166 //*****************************************************************************************************************
167 class LoaderThreads
: private ::std::vector
< LoadBinding
>
168 , private ThreadHelpBase
170 //-------------------------------------------------------------------------------------------------------------
173 //---------------------------------------------------------------------------------------------------------
174 inline LoaderThreads()
179 //---------------------------------------------------------------------------------------------------------
180 inline void append( const LoadBinding
& aBinding
)
182 ResetableGuard
aGuard( m_aLock
);
183 push_back( aBinding
);
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
)
190 ResetableGuard
aGuard( m_aLock
);
191 sal_Bool bFound
= sal_False
;
192 for( iterator pItem
=begin(); pItem
!=end(); ++pItem
)
194 if( pItem
->xListener
== rListener
)
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
)
209 ResetableGuard
aGuard( m_aLock
);
210 sal_Bool bFound
= sal_False
;
211 for( iterator pItem
=begin(); pItem
!=end(); ++pItem
)
213 if( pItem
->xLoader
== xLoader
)
224 //---------------------------------------------------------------------------------------------------------
225 // free ALL memory ... I hope it
228 ResetableGuard
aGuard( m_aLock
);
229 LoaderThreads().swap( *this );
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
242 "dispatch()" => should be you dispatch algorithm
243 "reactForLoadingState()" => do something depending from loading state ...
245 @implements XInterface
254 @devstatus ready to use
256 *//*-*************************************************************************************************************/
257 class BaseDispatcher
: // interfaces
258 public css::lang::XTypeProvider
,
259 public css::frame::XNotifyingDispatch
,
260 public css::frame::XLoadEventListener
, // => XEventListener too!
262 // Order is neccessary for right initialization!
263 protected ThreadHelpBase
,
264 protected TransactionBase
,
265 public ::cppu::OWeakObject
267 //-------------------------------------------------------------------------------------------------------------
269 //-------------------------------------------------------------------------------------------------------------
272 // constructor / destructor
273 BaseDispatcher( const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xFactory
,
274 const css::uno::Reference
< css::frame::XFrame
>& xOwnerFrame
);
276 void dispatchFinished ( const css::frame::DispatchResultEvent
& aEvent
, const css::uno::Reference
< css::frame::XDispatchResultListener
>& rListener
);
280 DECLARE_XTYPEPROVIDER
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
);
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
);
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
);
300 virtual void SAL_CALL
disposing ( const css::lang::EventObject
& aEvent
) throw( css::uno::RuntimeException
);
302 //-------------------------------------------------------------------------------------------------------------
304 //-------------------------------------------------------------------------------------------------------------
306 virtual ~BaseDispatcher();
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!)
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!)
325 @seealso method statusChanged()
326 @seealso method loadFinished()
327 @seealso method loadCancelled()
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
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
,
342 const css::uno::Any
& aAsyncInfo
) = 0;
344 virtual void SAL_CALL
reactForHandlingState( const css::util::URL
& aURL
,
345 const css::uno::Sequence
< css::beans::PropertyValue
>& lDescriptor
,
347 const css::uno::Any
& aAsyncInfo
) = 0;
349 //-------------------------------------------------------------------------------------------------------------
351 //-------------------------------------------------------------------------------------------------------------
353 ::rtl::OUString
implts_detectType ( const css::util::URL
& aURL
,
354 css::uno::Sequence
< css::beans::PropertyValue
>& lDescriptor
,
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
);
374 //-------------------------------------------------------------------------------------------------------------
376 // - should be private normaly ...
377 // - but some super classes need access to some of them => protected!
378 //-------------------------------------------------------------------------------------------------------------
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!)
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
387 }; // class BaseDispatcher
389 } // namespace framework
391 #endif // #ifndef __FRAMEWORK_DISPATCH_BASEDISPATCHER_HXX_