Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/framework/desktop.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#pragma once
21
22
#include <sal/config.h>
23
24
#include <memory>
25
#include <vector>
26
27
#include "framecontainer.hxx"
28
#include "fwkdllapi.h"
29
#include "transactionmanager.hxx"
30
31
#include <com/sun/star/frame/XUntitledNumbers.hpp>
32
#include <com/sun/star/frame/XDesktop2.hpp>
33
#include <com/sun/star/frame/XTerminateListener.hpp>
34
#include <com/sun/star/lang/XServiceInfo.hpp>
35
#include <com/sun/star/frame/XTasksSupplier.hpp>
36
#include <com/sun/star/frame/XDispatchResultListener.hpp>
37
#include <com/sun/star/task/XInteractionHandler.hpp>
38
39
#include <comphelper/multicontainer2.hxx>
40
#include <cppuhelper/basemutex.hxx>
41
#include <cppuhelper/compbase.hxx>
42
#include <cppuhelper/propshlp.hxx>
43
#include <rtl/ref.hxx>
44
45
class SvtCommandOptions;
46
47
namespace comphelper { class NumberedCollection; }
48
namespace com::sun::star::frame { class XDispatchRecorderSupplier; }
49
namespace com::sun::star::lang { class XComponent; }
50
namespace com::sun::star::uno { class XComponentContext; }
51
52
namespace framework{
53
54
class InterceptionHelper;
55
class OFrames;
56
57
enum ELoadState
58
{
59
    E_NOTSET      ,
60
    E_SUCCESSFUL  ,
61
    E_FAILED      ,
62
    E_INTERACTION
63
};
64
65
/*-************************************************************************************************************
66
    @short      implement the topframe of frame tree
67
    @descr      This is the root of the frame tree. The desktop has no window, is not visible but he is the logical
68
                "masternode" to build the hierarchy.
69
70
    @implements XInterface
71
                XTypeProvider
72
                XServiceInfo
73
                XDesktop
74
                XComponentLoader
75
                XTasksSupplier
76
                XDispatchProvider
77
                XFramesSupplier
78
                XFrame
79
                XComponent
80
                XPropertySet
81
                XFastPropertySet
82
                XMultiPropertySet
83
                XDispatchResultListener
84
                XEventListener
85
                XInteractionHandler
86
87
    @devstatus  ready to use
88
    @threadsafe yes
89
*//*-*************************************************************************************************************/
90
typedef cppu::WeakComponentImplHelper<
91
           css::lang::XServiceInfo              ,
92
           css::frame::XDesktop2                ,
93
           css::frame::XTasksSupplier           ,
94
           css::frame::XDispatchResultListener  ,   // => XEventListener
95
           css::task::XInteractionHandler       ,
96
           css::frame::XUntitledNumbers > Desktop_BASE;
97
98
class FWK_DLLPUBLIC Desktop final : private cppu::BaseMutex,
99
                public Desktop_BASE,
100
                public cppu::OPropertySetHelper
101
{
102
    // internal used types, const etcpp.
103
    private:
104
105
        /** used temporary to know which listener was already called or not. */
106
        typedef ::std::vector< css::uno::Reference< css::frame::XTerminateListener > > TTerminateListenerList;
107
108
    // public methods
109
    public:
110
111
        //  constructor / destructor
112
                 Desktop( css::uno::Reference< css::uno::XComponentContext >  xContext );
113
        virtual ~Desktop(                                                                    ) override;
114
115
        void constructorInit();
116
117
        //  XServiceInfo
118
        virtual OUString SAL_CALL getImplementationName() override;
119
120
        virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override;
121
122
        virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
123
124
        // XInterface
125
        virtual void SAL_CALL acquire() noexcept override
126
30.7M
            { OWeakObject::acquire(); }
127
        virtual void SAL_CALL release() noexcept override
128
30.7M
            { OWeakObject::release(); }
129
        virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type& type) override;
130
131
        // XTypeProvider
132
        virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes(  ) override;
133
134
        /**
135
            @interface  XDesktop
136
137
            @short      try to shutdown these desktop environment.
138
139
            @descr      Will try to close all frames. If at least one frame could
140
                        not be closed successfully termination will be stopped.
141
142
                        Registered termination listener will be taken into account
143
                        also. As special feature some of our registered listener
144
                        are well known by its UNO implementation name. They are handled
145
                        different to all other listener.
146
147
                        Btw: Desktop.terminate() was designed in the past to be used
148
                        within an UI based environment. So it's allowed e.g. to
149
                        call XController.suspend() here. If UI isn't an option ... please
150
                        use XCloseable.close() at these desktop implementation.
151
                        ... if it will be supported in the future .-))
152
153
            @seealso    XTerminateListener
154
            @seealso    XTerminateListener2
155
156
            @return     true if all open frames could be closed and no listener threw
157
                        a veto exception; false otherwise.
158
159
            @onerror    False will be returned.
160
            @threadsafe yes
161
         */
162
        virtual sal_Bool SAL_CALL terminate() override;
163
164
        /**
165
            @interface  XDesktop
166
167
            @short      add a listener for termination events
168
169
            @descr      Additional to adding normal listener these method was implemented special.
170
                        Every listener will be asked for its uno implementation name.
171
                        Some of them are well known... and the corresponding listener won't be added
172
                        to the container of "normal listener". Those listener will be set as special
173
                        member.
174
                        see e.g. member m_xSfxTerminator
175
176
            @seealso    terminate()
177
178
            @param      xListener
179
                        the listener for registration.
180
181
            @threadsafe yes
182
         */
183
        virtual void SAL_CALL addTerminateListener( const css::uno::Reference< css::frame::XTerminateListener >& xListener ) override;
184
185
        /**
186
            @interface  XDesktop
187
188
            @short      remove a listener from this container.
189
190
            @descr      Additional to removing normal listener these method was implemented special.
191
                        Every listener will be asked for its uno implementation name.
192
                        Some of them are well known... and the corresponding listener was set as special member.
193
                        Now those special member will be reset also.
194
                        see e.g. member m_xSfxTerminator
195
196
            @seealso    terminate()
197
198
            @param      xListener
199
                        the listener for deregistration.
200
201
            @threadsafe yes
202
         */
203
        virtual void SAL_CALL removeTerminateListener( const css::uno::Reference< css::frame::XTerminateListener >& xListener ) override;
204
205
        virtual css::uno::Reference< css::container::XEnumerationAccess >           SAL_CALL getComponents              (                                                                                 ) override;
206
        virtual css::uno::Reference< css::lang::XComponent >                        SAL_CALL getCurrentComponent        (                                                                                 ) override;
207
        virtual css::uno::Reference< css::frame::XFrame >                           SAL_CALL getCurrentFrame            (                                                                                 ) override;
208
209
        //  XComponentLoader
210
        virtual css::uno::Reference< css::lang::XComponent >                        SAL_CALL loadComponentFromURL       ( const OUString&                                         sURL             ,
211
                                                                                                                          const OUString&                                         sTargetFrameName ,
212
                                                                                                                                sal_Int32                                                nSearchFlags     ,
213
                                                                                                                          const css::uno::Sequence< css::beans::PropertyValue >&         lArguments       ) override;
214
215
        //  XTasksSupplier
216
        virtual css::uno::Reference< css::container::XEnumerationAccess >           SAL_CALL getTasks                   (                                                                                 ) override;
217
        virtual css::uno::Reference< css::frame::XTask >                            SAL_CALL getActiveTask              (                                                                                 ) override;
218
219
        //  XDispatchProvider
220
        virtual css::uno::Reference< css::frame::XDispatch >                        SAL_CALL queryDispatch              ( const css::util::URL&                                          aURL             ,
221
                                                                                                                          const OUString&                                         sTargetFrameName ,
222
                                                                                                                                sal_Int32                                                nSearchFlags     ) override;
223
        virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > >  SAL_CALL queryDispatches            ( const css::uno::Sequence< css::frame::DispatchDescriptor >&    lQueries         ) override;
224
225
        // XDispatchProviderInterception
226
        virtual void                                                                SAL_CALL registerDispatchProviderInterceptor( const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor) override;
227
        virtual void                                                                SAL_CALL releaseDispatchProviderInterceptor ( const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor) override;
228
229
        //  XFramesSupplier
230
        virtual css::uno::Reference< css::frame::XFrames >                          SAL_CALL getFrames                  (                                                                                 ) override;
231
        virtual css::uno::Reference< css::frame::XFrame >                           SAL_CALL getActiveFrame             (                                                                                 ) override;
232
        virtual void                                                                SAL_CALL setActiveFrame             ( const css::uno::Reference< css::frame::XFrame >&               xFrame           ) override;
233
234
        //   XFrame
235
        //  Attention: findFrame() is implemented only! Other methods make no sense for our desktop!
236
        virtual css::uno::Reference< css::frame::XFrame >                           SAL_CALL findFrame                  ( const OUString&                                         sTargetFrameName ,
237
                                                                                                                                sal_Int32                                                nSearchFlags     ) override;
238
        virtual void                                                                SAL_CALL initialize                 ( const css::uno::Reference< css::awt::XWindow >&                xWindow          ) override;
239
        virtual css::uno::Reference< css::awt::XWindow >                            SAL_CALL getContainerWindow         (                                                                                 ) override;
240
        virtual void                                                                SAL_CALL setCreator                 ( const css::uno::Reference< css::frame::XFramesSupplier >&      xCreator         ) override;
241
        virtual css::uno::Reference< css::frame::XFramesSupplier >                  SAL_CALL getCreator                 (                                                                                 ) override;
242
        virtual OUString                                                     SAL_CALL getName                    (                                                                                 ) override;
243
        virtual void                                                                SAL_CALL setName                    ( const OUString&                                         sName            ) override;
244
        virtual sal_Bool                                                            SAL_CALL isTop                      (                                                                                 ) override;
245
        virtual void                                                                SAL_CALL activate                   (                                                                                 ) override;
246
        virtual void                                                                SAL_CALL deactivate                 (                                                                                 ) override;
247
        virtual sal_Bool                                                            SAL_CALL isActive                   (                                                                                 ) override;
248
        virtual sal_Bool                                                            SAL_CALL setComponent               ( const css::uno::Reference< css::awt::XWindow >&                xComponentWindow ,
249
                                                                                                                          const css::uno::Reference< css::frame::XController >&          xController      ) override;
250
        virtual css::uno::Reference< css::awt::XWindow >                            SAL_CALL getComponentWindow         (                                                                                 ) override;
251
        virtual css::uno::Reference< css::frame::XController >                      SAL_CALL getController              (                                                                                 ) override;
252
        virtual void                                                                SAL_CALL contextChanged             (                                                                                 ) override;
253
        virtual void                                                                SAL_CALL addFrameActionListener     ( const css::uno::Reference< css::frame::XFrameActionListener >& xListener        ) override;
254
        virtual void                                                                SAL_CALL removeFrameActionListener  ( const css::uno::Reference< css::frame::XFrameActionListener >& xListener        ) override;
255
256
        //   XComponent
257
        virtual void SAL_CALL disposing() override;
258
        virtual void                                                                SAL_CALL addEventListener           ( const css::uno::Reference< css::lang::XEventListener >&        xListener        ) override;
259
        virtual void                                                                SAL_CALL removeEventListener        ( const css::uno::Reference< css::lang::XEventListener >&        xListener        ) override;
260
261
        //   XDispatchResultListener
262
        virtual void SAL_CALL dispatchFinished      ( const css::frame::DispatchResultEvent&                    aEvent     ) override;
263
264
        //   XEventListener
265
        virtual void                                                                SAL_CALL disposing                  ( const css::lang::EventObject&                                  aSource          ) override;
266
267
        //   XInteractionHandler
268
        virtual void                                                                SAL_CALL handle                     ( const css::uno::Reference< css::task::XInteractionRequest >&   xRequest         ) override;
269
270
        // css.frame.XUntitledNumbers
271
        virtual ::sal_Int32 SAL_CALL leaseNumber( const css::uno::Reference< css::uno::XInterface >& xComponent ) override;
272
273
        // css.frame.XUntitledNumbers
274
        virtual void SAL_CALL releaseNumber( ::sal_Int32 nNumber ) override;
275
276
        // css.frame.XUntitledNumbers
277
        virtual void SAL_CALL releaseNumberForComponent( const css::uno::Reference< css::uno::XInterface >& xComponent ) override;
278
279
        // css.frame.XUntitledNumbers
280
        virtual OUString SAL_CALL getUntitledPrefix() override;
281
282
        // we need this wrapped terminate()-call to terminate even the QuickStarter
283
        // non-virtual and non-UNO for now
284
        /// @throws css::uno::RuntimeException
285
        bool terminateQuickstarterToo();
286
287
        void shutdown();
288
289
    private:
290
        //  OPropertySetHelper
291
        virtual sal_Bool                                            SAL_CALL convertFastPropertyValue        (       css::uno::Any&  aConvertedValue ,
292
                                                                                                                     css::uno::Any&  aOldValue       ,
293
                                                                                                                     sal_Int32       nHandle         ,
294
                                                                                                               const css::uno::Any&  aValue          ) override;
295
        virtual void                                                SAL_CALL setFastPropertyValue_NoBroadcast(       sal_Int32       nHandle         ,
296
                                                                                                               const css::uno::Any&  aValue          ) override;
297
        using cppu::OPropertySetHelper::getFastPropertyValue;
298
        virtual void                                                SAL_CALL getFastPropertyValue            (       css::uno::Any&  aValue          ,
299
                                                                                                                     sal_Int32       nHandle         ) const override;
300
        virtual ::cppu::IPropertyArrayHelper&                       SAL_CALL getInfoHelper                   (                                       ) override;
301
        virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo              (                                       ) override;
302
303
        css::uno::Reference< css::lang::XComponent >            impl_getFrameComponent          ( const css::uno::Reference< css::frame::XFrame >&  xFrame          ) const;
304
305
        /** calls queryTermination() on every registered termination listener.
306
         *
307
         *  Note: Only normal termination listener (registered in list m_aListenerContainer
308
         *        will be recognized here. Special listener like quick starter, pipe or others
309
         *        has to be handled explicitly !
310
         *
311
         *  @param  [out] lCalledListener
312
         *          every called listener will be returned here.
313
         *          Those list will be used to inform all called listener
314
         *          about cancel this termination request.
315
         *
316
         *  @return true if no one vetoed the termination.
317
         *
318
         *  @see    impl_sendCancelTerminationEvent()
319
         */
320
        bool impl_sendQueryTerminationEvent(TTerminateListenerList& lCalledListener);
321
322
        /** calls cancelTermination() on every termination listener
323
         *  where queryTermination() was called before.
324
         *
325
         *  Note: Only normal termination listener (registered in list m_aListenerContainer
326
         *        will be recognized here. Special listener like quick starter, pipe or others
327
         *        has to be handled explicitly !
328
         *
329
         *  @param  [in] lCalledListener
330
         *          every listener in this list was called within its method
331
         *          queryTermination() before.
332
         *
333
         *  @see    impl_sendQueryTerminationEvent()
334
         */
335
        void impl_sendCancelTerminationEvent(const TTerminateListenerList& lCalledListener);
336
337
        /** calls notifyTermination() on the clipboard listener
338
         *
339
         * The system clipboard may decide that it wants copies
340
         * in several formats of the clipboard content requiring
341
         * nearly all the services
342
         *
343
         */
344
        void impl_sendTerminateToClipboard();
345
346
        /** calls notifyTermination() on every registered termination listener.
347
         *
348
         *  Note: Only normal termination listener (registered in list m_aListenerContainer
349
         *        will be recognized here. Special listener like quick starter, pipe or others
350
         *        has to be handled explicitly !
351
         */
352
        void impl_sendNotifyTerminationEvent();
353
354
        /** try to close all open frames.
355
         *
356
         *  Iterates over all child frames and try to close them.
357
         *  Given parameter bAllowUI enable/disable showing any UI
358
         *  (which mostly occur on calling XController->suspend()).
359
         *
360
         *  This method doesn't stop if one frame could not be closed.
361
         *  It will ignore such frames and try all other ones.
362
         *  But it returns false in such case - true otherwise.
363
         *
364
         *  @param  bAllowUI
365
         *          enable/disable showing of UI.
366
         *
367
         *  @return true if all frames could be closed; false otherwise.
368
         */
369
        bool impl_closeFrames(bool bAllowUI);
370
371
    private:
372
373
        mutable TransactionManager    m_aTransactionManager;
374
375
        /** check flag to protect against multiple terminate runs
376
          */
377
        bool m_bIsTerminated;
378
379
        /** check flag to protect us against dispose before terminate!
380
          *   see dispose() for further information!
381
          */
382
        bool m_bIsShutdown;
383
384
        /** when true, the call came from session manager
385
          *   the method is Desktop::terminateQuickstarterToo()
386
          *   this the only one place where set this to true
387
          *   In this case,  when one frame break, not make
388
          *   question for other, the break of shutdown or logout
389
          *   can be only once.
390
          *   In Desktop::impl_closeFrames would be test and break
391
          *   the loop and reset to false
392
          */
393
        bool m_bSession;
394
395
        css::uno::Reference< css::uno::XComponentContext >              m_xContext;               /// reference to factory, which has create this instance
396
        FrameContainer                                                  m_aChildTaskContainer;    /// array of child tasks (children of desktop are tasks; and tasks are also frames - But pure frames are not accepted!)
397
        comphelper::OMultiTypeInterfaceContainerHelper2                 m_aListenerContainer;     /// container for ALL Listener
398
        rtl::Reference< OFrames >                                       m_xFramesHelper;          /// helper for XFrames, XIndexAccess, XElementAccess and implementation of a childcontainer!
399
        rtl::Reference< InterceptionHelper >                            m_xDispatchHelper;        /// helper to dispatch something for new tasks, created by "_blank"!
400
        ELoadState                                                      m_eLoadState;             /// hold information about state of asynchron loading of component for loadComponentFromURL()!
401
        bool                                                            m_bSuspendQuickstartVeto; /// don't ask quickstart for a veto
402
        std::unique_ptr<SvtCommandOptions>                              m_xCommandOptions;        /// ref counted class to support disabling commands defined by configuration file
403
        OUString                                                        m_sName;
404
        OUString                                                        m_sTitle;
405
        css::uno::Reference< css::frame::XDispatchRecorderSupplier >    m_xDispatchRecorderSupplier;
406
407
        /** special terminate listener to close pipe and block external requests
408
          * during/after termination process is/was running
409
          */
410
        css::uno::Reference< css::frame::XTerminateListener > m_xPipeTerminator;
411
412
        /** special terminate listener shown inside system tray (quick starter)
413
          * Will hinder the office on shutdown ... but wish to allow closing
414
          * of open documents. And because that's different to a normal terminate listener
415
          * it has to be handled special .-)
416
          */
417
        css::uno::Reference< css::frame::XTerminateListener > m_xQuickLauncher;
418
419
        /** special terminate listener which loads images asynchronous for current open documents.
420
          * Because internally it uses blocking system APIs... it can't be guaranteed that
421
          * running jobs can be cancelled successfully if the corresponding document will be closed...
422
          * it will not hinder those documents on closing. Instead it let all jobs running...
423
          * but at least on terminate we have to wait for all those blocked requests.
424
          * So these implementation must be a special terminate listener too .-(
425
          */
426
        css::uno::Reference< css::frame::XTerminateListener > m_xSWThreadManager;
427
428
        /** special terminate listener shutting down the SfxApplication.
429
          * Because these desktop instance closes documents and informs listener
430
          * only... it does not really shutdown the whole application.
431
          *
432
          * Btw: that wouldn't be possible by design... because Desktop.terminate()
433
          * has to return a boolean value about success... it can't really shutdown the
434
          * process .-)
435
          *
436
          * So we uses a trick: a special listener (exactly these one here) listen for notifyTermination()
437
          * and shutdown the process asynchronous. But desktop has to make this special
438
          * notification as really last one ... Otherwise it can happen that asynchronous
439
          * shutdown will be faster than all other code around Desktop.terminate() .-))
440
          */
441
        css::uno::Reference< css::frame::XTerminateListener > m_xSfxTerminator;
442
443
        rtl::Reference< ::comphelper::NumberedCollection > m_xTitleNumberGenerator;
444
445
        std::vector<css::uno::Reference<css::frame::XTerminateListener>> m_xComponentDllListeners;
446
447
};      //  class Desktop
448
449
FWK_DLLPUBLIC const rtl::Reference<Desktop> & getDesktop(
450
    css::uno::Reference<css::uno::XComponentContext> const & context);
451
452
}       //  namespace framework
453
454
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */