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