1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef __FRAMEWORK_DISPATCH_CLOSEDISPATCHER_HXX_
29*cdf0e10cSrcweir #define __FRAMEWORK_DISPATCH_CLOSEDISPATCHER_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_______________________________________________
32*cdf0e10cSrcweir // my own includes
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <threadhelp/threadhelpbase.hxx>
35*cdf0e10cSrcweir #include <macros/xinterface.hxx>
36*cdf0e10cSrcweir #include <macros/xtypeprovider.hxx>
37*cdf0e10cSrcweir #include <macros/debug.hxx>
38*cdf0e10cSrcweir #include <macros/generic.hxx>
39*cdf0e10cSrcweir #include <stdtypes.h>
40*cdf0e10cSrcweir #include <general.h>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir //_______________________________________________
43*cdf0e10cSrcweir // interface includes
44*cdf0e10cSrcweir #include <com/sun/star/lang/XTypeProvider.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/frame/XStatusListener.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/frame/XNotifyingDispatch.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchInformationProvider.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/util/URL.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchResultListener.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/frame/DispatchResultState.hpp>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //_______________________________________________
55*cdf0e10cSrcweir // other includes
56*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
57*cdf0e10cSrcweir #include <vcl/evntpost.hxx>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir //_______________________________________________
60*cdf0e10cSrcweir // namespace
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir namespace framework{
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir //-----------------------------------------------
65*cdf0e10cSrcweir /**
66*cdf0e10cSrcweir     @short          helper to dispatch the URLs ".uno:CloseDoc"/".uno:CloseWin"/".uno:CloseFrame"
67*cdf0e10cSrcweir                     to close a frame/document or the whole application implicitly in case it was the last frame
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     @descr          These URLs implements a special functionality to close a document or the whole frame ...
70*cdf0e10cSrcweir                     and handle the state, it was the last frame or document. Then we create the
71*cdf0e10cSrcweir                     default backing document which can be used to open new ones using the file open dialog
72*cdf0e10cSrcweir                     or some other menu entries. Or we terminate the whole application in case this backing mode shouldnt
73*cdf0e10cSrcweir                     be used.
74*cdf0e10cSrcweir  */
75*cdf0e10cSrcweir class CloseDispatcher : public css::lang::XTypeProvider
76*cdf0e10cSrcweir                       , public css::frame::XNotifyingDispatch             // => XDispatch
77*cdf0e10cSrcweir                       , public css::frame::XDispatchInformationProvider
78*cdf0e10cSrcweir                         // baseclasses ... order is neccessary for right initialization!
79*cdf0e10cSrcweir                       , private ThreadHelpBase
80*cdf0e10cSrcweir                       , public  ::cppu::OWeakObject
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir     //-------------------------------------------
83*cdf0e10cSrcweir     // types
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     private:
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         //---------------------------------------
88*cdf0e10cSrcweir         /** @short  describe, which request must be done here.
89*cdf0e10cSrcweir         @descr      The incoming URLs {.uno:CloseDoc/CloseWin and CloseFrame
90*cdf0e10cSrcweir                     can be classified so and checked later performant.}*/
91*cdf0e10cSrcweir         enum EOperation
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             E_CLOSE_DOC,
94*cdf0e10cSrcweir             E_CLOSE_FRAME,
95*cdf0e10cSrcweir             E_CLOSE_WIN
96*cdf0e10cSrcweir         };
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     //-------------------------------------------
99*cdf0e10cSrcweir     // member
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     private:
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         //---------------------------------------
104*cdf0e10cSrcweir         /** @short reference to an uno service manager,
105*cdf0e10cSrcweir                    which can be used to create own needed
106*cdf0e10cSrcweir                    uno resources. */
107*cdf0e10cSrcweir         css::uno::Reference< css::lang::XMultiServiceFactory > m_xSMGR;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         //---------------------------------------
110*cdf0e10cSrcweir         /** @short  reference to the target frame, which should be
111*cdf0e10cSrcweir                     closed by this dispatch. */
112*cdf0e10cSrcweir         css::uno::WeakReference< css::frame::XFrame > m_xCloseFrame;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         //---------------------------------------
115*cdf0e10cSrcweir         /** @short  used for asynchronous callbacks within the main thread.
116*cdf0e10cSrcweir             @descr  Internaly we work asynchronous. Because our callis
117*cdf0e10cSrcweir                     are not aware, that her request can kill its own environment ... */
118*cdf0e10cSrcweir         ::vcl::EventPoster m_aAsyncCallback;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         //---------------------------------------
121*cdf0e10cSrcweir         /** @short  used inside asyncronous callback to decide,
122*cdf0e10cSrcweir                     which operation must be executed. */
123*cdf0e10cSrcweir         EOperation m_eOperation;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         //---------------------------------------
126*cdf0e10cSrcweir         /** @short  for asynchronous operations we must hold us self alive! */
127*cdf0e10cSrcweir         css::uno::Reference< css::uno::XInterface > m_xSelfHold;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         //---------------------------------------
130*cdf0e10cSrcweir         /** @short  list of registered status listener */
131*cdf0e10cSrcweir         ListenerHash m_lStatusListener;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         //---------------------------------------
134*cdf0e10cSrcweir         /** @short  holded alive for internaly asynchronous operations! */
135*cdf0e10cSrcweir         css::uno::Reference< css::frame::XDispatchResultListener > m_xResultListener;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     //-------------------------------------------
138*cdf0e10cSrcweir     // native interface
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	public:
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         //---------------------------------------
143*cdf0e10cSrcweir         /** @short  connect a new CloseDispatcher instance to its frame.
144*cdf0e10cSrcweir             @descr  One CloseDispatcher instance is bound to onw frame only.
145*cdf0e10cSrcweir                     That makes an implementation (e.g. of listener support)
146*cdf0e10cSrcweir                     much more easier .-)
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir             @param  xSMGR
149*cdf0e10cSrcweir                     an un oservice manager, which is needed to create uno resource
150*cdf0e10cSrcweir                     internaly.
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir             @param  xFrame
153*cdf0e10cSrcweir                     the frame where the corresponding dispatch was started.
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir             @param  sTarget
156*cdf0e10cSrcweir                     help us to find the right target for this close operation.
157*cdf0e10cSrcweir          */
158*cdf0e10cSrcweir         CloseDispatcher(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR  ,
159*cdf0e10cSrcweir                         const css::uno::Reference< css::frame::XFrame >&              xFrame ,
160*cdf0e10cSrcweir                         const ::rtl::OUString&                                        sTarget);
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         //---------------------------------------
163*cdf0e10cSrcweir         /** @short  does nothing real. */
164*cdf0e10cSrcweir         virtual ~CloseDispatcher();
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     //-------------------------------------------
167*cdf0e10cSrcweir     // uno interface
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     public:
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir         //---------------------------------------
172*cdf0e10cSrcweir         FWK_DECLARE_XINTERFACE
173*cdf0e10cSrcweir         FWK_DECLARE_XTYPEPROVIDER
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir         //---------------------------------------
176*cdf0e10cSrcweir         // XNotifyingDispatch
177*cdf0e10cSrcweir         virtual void SAL_CALL dispatchWithNotification( const css::util::URL&                                             aURL      ,
178*cdf0e10cSrcweir                                                         const css::uno::Sequence< css::beans::PropertyValue >&            lArguments,
179*cdf0e10cSrcweir                                                         const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw(css::uno::RuntimeException);
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         //---------------------------------------
182*cdf0e10cSrcweir         // XDispatch
183*cdf0e10cSrcweir         virtual void SAL_CALL dispatch            ( const css::util::URL&                                     aURL      ,
184*cdf0e10cSrcweir                                                     const css::uno::Sequence< css::beans::PropertyValue >&    lArguments) throw(css::uno::RuntimeException);
185*cdf0e10cSrcweir         virtual void SAL_CALL addStatusListener   ( const css::uno::Reference< css::frame::XStatusListener >& xListener ,
186*cdf0e10cSrcweir                                                     const css::util::URL&                                     aURL      ) throw(css::uno::RuntimeException);
187*cdf0e10cSrcweir         virtual void SAL_CALL removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >& xListener ,
188*cdf0e10cSrcweir                                                     const css::util::URL&                                     aURL      ) throw(css::uno::RuntimeException);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir         //---------------------------------------
191*cdf0e10cSrcweir         // XDispatchInformationProvider
192*cdf0e10cSrcweir         virtual css::uno::Sequence< sal_Int16 >                       SAL_CALL getSupportedCommandGroups         (                         ) throw (css::uno::RuntimeException);
193*cdf0e10cSrcweir         virtual css::uno::Sequence< css::frame::DispatchInformation > SAL_CALL getConfigurableDispatchInformation( sal_Int16 nCommandGroup ) throw (css::uno::RuntimeException);
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     //-------------------------------------------
196*cdf0e10cSrcweir     // internal helper
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     private:
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         //---------------------------------------
201*cdf0e10cSrcweir         /** @short  a callback for asynchronous started operations.
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir             @descr  As already mentione, we make internaly all operations
204*cdf0e10cSrcweir                     asynchronous. Otherwhise our callis kill its own environment
205*cdf0e10cSrcweir                     during they call us ...
206*cdf0e10cSrcweir         */
207*cdf0e10cSrcweir         DECL_LINK( impl_asyncCallback, void* );
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         //---------------------------------------
210*cdf0e10cSrcweir         /** @short  prepare m_xCloseFrame so it should be closeable without problems.
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir             @descr  Thats needed to be shure, that the document cant disagree
213*cdf0e10cSrcweir                     later with e.g. an office termination.
214*cdf0e10cSrcweir                     The problem: Closing of documents can show UI. If the user
215*cdf0e10cSrcweir                     ignores it and open/close other documents, we cant know
216*cdf0e10cSrcweir                     which state the office has after closing of this frame.
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir             @param  bAllowSuspend
219*cdf0e10cSrcweir                     force calling of XController->suspend().
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir             @param  bCloseAllOtherViewsToo
222*cdf0e10cSrcweir                     if there are other top level frames, which
223*cdf0e10cSrcweir                     contains views to the same document then our m_xCloseFrame,
224*cdf0e10cSrcweir                     they are forced to be closed too.
225*cdf0e10cSrcweir                     We need it to implement the CLOSE_DOC semantic.
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir             @return [boolean]
228*cdf0e10cSrcweir                     sal_True if closing was successfully.
229*cdf0e10cSrcweir          */
230*cdf0e10cSrcweir         sal_Bool implts_prepareFrameForClosing(const css::uno::Reference< css::frame::XFrame >& xFrame                ,
231*cdf0e10cSrcweir                                                      sal_Bool                                   bAllowSuspend         ,
232*cdf0e10cSrcweir                                                      sal_Bool                                   bCloseAllOtherViewsToo,
233*cdf0e10cSrcweir                                                      sal_Bool&                                  bControllerSuspended  );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         //---------------------------------------
236*cdf0e10cSrcweir         /** @short  close the member m_xCloseFrame.
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir             @descr  This method does not look for any document
239*cdf0e10cSrcweir                     inside this frame. Such views must be cleared
240*cdf0e10cSrcweir                     before (e.g. by calling implts_closeView()!
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir                     Otherwhise e.g. the XController->suspend()
243*cdf0e10cSrcweir                     call isnt made and no UI warn the user about
244*cdf0e10cSrcweir                     loosing document changes. Because the
245*cdf0e10cSrcweir                     frame is closed ....
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir             @return [bool]
248*cdf0e10cSrcweir                     sal_True if closing was successfully.
249*cdf0e10cSrcweir          */
250*cdf0e10cSrcweir         sal_Bool implts_closeFrame();
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         //---------------------------------------
253*cdf0e10cSrcweir         /** @short  set the special BackingComponent (now StartModule)
254*cdf0e10cSrcweir                     as new component of our m_xCloseFrame.
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir             @return [bool]
257*cdf0e10cSrcweir                     sal_True if operation was successfully.
258*cdf0e10cSrcweir          */
259*cdf0e10cSrcweir         sal_Bool implts_establishBackingMode();
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir         //---------------------------------------
262*cdf0e10cSrcweir         /** @short  calls XDesktop->terminate().
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir             @descr  No office code has to be called
265*cdf0e10cSrcweir                     afterwards! Because the process is dieing ...
266*cdf0e10cSrcweir                     The only exception is a might be registered
267*cdf0e10cSrcweir                     listener at this instance here.
268*cdf0e10cSrcweir                     Because he should know, that such things will happen :-)
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir             @return [bool]
271*cdf0e10cSrcweir                     sal_True if termination of the application was started ...
272*cdf0e10cSrcweir          */
273*cdf0e10cSrcweir         sal_Bool implts_terminateApplication();
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir         //---------------------------------------
276*cdf0e10cSrcweir         /** @short  notify a DispatchResultListener.
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir             @descr  We check the listener reference before we use it.
279*cdf0e10cSrcweir                     So this method can be called everytimes!
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir             @parama xListener
282*cdf0e10cSrcweir                     the listener, which should be notified.
283*cdf0e10cSrcweir                     Can be null!
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir             @param  nState
286*cdf0e10cSrcweir                     directly used as css::frame::DispatchResultState value.
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir             @param  aResult
289*cdf0e10cSrcweir                     not used yet realy ...
290*cdf0e10cSrcweir          */
291*cdf0e10cSrcweir         void implts_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener >& xListener,
292*cdf0e10cSrcweir                                                sal_Int16                                                   nState   ,
293*cdf0e10cSrcweir                                          const css::uno::Any&                                              aResult  );
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir         //---------------------------------------
296*cdf0e10cSrcweir         /** @short  try to find the right target frame where this close request
297*cdf0e10cSrcweir                     must be realy done.
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir             @descr  The problem behind: closing some resources depends sometimes from the
300*cdf0e10cSrcweir                     context where its dispatched. Sometimes the start frame of the dispatch
301*cdf0e10cSrcweir                     has to be closed itself (target=_self) ... sometimes it's parent frame
302*cdf0e10cSrcweir                     has to be closed - BUT(!) it means a parent frame containing a top level
303*cdf0e10cSrcweir                     window. _top cant be used then for dispatch - because it adress TopFrames
304*cdf0e10cSrcweir                     not frames containg top level windows. So normaly _magic (which btw does not
305*cdf0e10cSrcweir                     exists at the moment .-) ) should be used. So we interpret target=<empty>
306*cdf0e10cSrcweir                     as _magic !
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir             @param  xFrame
309*cdf0e10cSrcweir                     start point for search of right dispatch frame.
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir             @param  sTarget
312*cdf0e10cSrcweir                     give us an idea how this target frame must be searched.
313*cdf0e10cSrcweir         */
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir         static css::uno::Reference< css::frame::XFrame > static_impl_searchRightTargetFrame(const css::uno::Reference< css::frame::XFrame >& xFrame ,
316*cdf0e10cSrcweir                                                                                             const ::rtl::OUString&                           sTarget);
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir }; // class CloseDispatcher
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir } // namespace framework
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir #endif // #ifndef __FRAMEWORK_DISPATCH_CLOSEDISPATCHER_HXX_
323