nss: upgrade to release 3.73
[LibreOffice.git] / sfx2 / inc / preventduplicateinteraction.hxx
blobedd503d0129eee9303e912655e4a4a23f2b37b10
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_FRAMEWORK_PREVENTDUPLICATEINTERACTION_HXX
21 #define INCLUDED_FRAMEWORK_PREVENTDUPLICATEINTERACTION_HXX
23 #include <vector>
25 #include <com/sun/star/frame/Desktop.hpp>
26 #include <com/sun/star/frame/TerminationVetoException.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/task/XInteractionHandler2.hpp>
29 #include <com/sun/star/task/XInteractionRequest.hpp>
31 #include <cppuhelper/compbase.hxx>
32 #include <cppuhelper/implbase.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
35 #include <vcl/wrkwin.hxx>
36 #include <vcl/svapp.hxx>
38 namespace com::sun::star::uno {
39 class XComponentContext;
42 namespace sfx2 {
44 inline void closedialogs(SystemWindow& rTopLevel, bool bCloseRoot)
46 for (vcl::Window *pChild = rTopLevel.GetWindow(GetWindowType::FirstTopWindowChild); pChild; pChild = rTopLevel.GetWindow(GetWindowType::NextTopWindowSibling))
47 closedialogs(dynamic_cast<SystemWindow&>(*pChild), true);
48 if (bCloseRoot)
49 rTopLevel.Close();
52 // This is intended to be the parent for any warning dialogs launched
53 // during the load of a document so that those dialogs are modal to
54 // this window and don't block any existing windows.
56 // If there are dialog children open on exit then veto termination,
57 // close the topmost dialog and retry termination.
58 class WarningDialogsParent final :
59 public cppu::WeakComponentImplHelper<css::frame::XTerminateListener>
61 private:
62 osl::Mutex m_aLock;
63 VclPtr<WorkWindow> m_xWin;
64 css::uno::Reference<css::awt::XWindow> m_xInterface;
66 private:
68 DECL_STATIC_LINK(WarningDialogsParent, TerminateDesktop, void*, void);
70 void closewarningdialogs()
72 if (!m_xWin)
73 return;
74 SolarMutexGuard aSolarGuard;
75 closedialogs(dynamic_cast<SystemWindow&>(*m_xWin), false);
78 public:
80 using cppu::WeakComponentImplHelperBase::disposing;
81 virtual void SAL_CALL disposing(const css::lang::EventObject&) override
85 // XTerminateListener
86 virtual void SAL_CALL queryTermination(const css::lang::EventObject&) override
88 closewarningdialogs();
89 Application::PostUserEvent(LINK(this, WarningDialogsParent, TerminateDesktop));
90 throw css::frame::TerminationVetoException();
93 virtual void SAL_CALL notifyTermination(const css::lang::EventObject&) override
97 public:
98 WarningDialogsParent()
99 : cppu::WeakComponentImplHelper<css::frame::XTerminateListener>(m_aLock)
101 SolarMutexGuard aSolarGuard;
102 m_xWin = VclPtr<WorkWindow>::Create(nullptr, WB_STDWORK);
103 m_xWin->SetText("dialog parent for warning dialogs during load");
104 m_xInterface = VCLUnoHelper::GetInterface(m_xWin);
107 virtual ~WarningDialogsParent() override
109 closewarningdialogs();
110 m_xWin.disposeAndClear();
113 const css::uno::Reference<css::awt::XWindow>& GetDialogParent() const
115 return m_xInterface;
119 class WarningDialogsParentScope
121 private:
122 css::uno::Reference<css::frame::XDesktop> m_xDesktop;
123 rtl::Reference<WarningDialogsParent> m_xListener;
125 public:
126 WarningDialogsParentScope(const css::uno::Reference<css::uno::XComponentContext>& rContext)
127 : m_xDesktop(css::frame::Desktop::create(rContext), css::uno::UNO_QUERY_THROW)
128 , m_xListener(new WarningDialogsParent)
130 m_xDesktop->addTerminateListener(m_xListener.get());
133 const css::uno::Reference<css::awt::XWindow>& GetDialogParent() const
135 return m_xListener->GetDialogParent();
138 ~WarningDialogsParentScope()
140 m_xDesktop->removeTerminateListener(m_xListener.get());
145 @short Prevent us from showing the same interaction more than once during
146 the same transaction.
148 @descr Every interaction provided to this helper will be safed ... handled by the internal
149 used UUIInteractionHandler (!) and never be handled a second time!
151 On the other side there exists some interactions, which allow a retry.
152 So this helper allow to set a list of interactions combined with a retry value.
154 struct ThreadHelpBase2
156 public:
157 mutable ::osl::Mutex m_aLock;
160 class PreventDuplicateInteraction final : private ThreadHelpBase2
161 , public ::cppu::WeakImplHelper<css::lang::XInitialization, css::task::XInteractionHandler2>
164 // structs, types etc.
165 public:
167 struct InteractionInfo
169 public:
170 /// describe the interaction.
171 css::uno::Type m_aInteraction;
172 /// after max count was reached this interaction will be blocked.
173 sal_Int32 m_nMaxCount;
174 /// count how often this interaction was called.
175 sal_Int32 m_nCallCount;
176 /** hold the last intercepted request (matching the set interaction type) alive
177 so it can be used for further checks */
178 css::uno::Reference< css::task::XInteractionRequest > m_xRequest;
180 public:
182 InteractionInfo(const css::uno::Type& aInteraction)
183 : m_aInteraction(aInteraction)
184 , m_nMaxCount (1 )
185 , m_nCallCount (0 )
189 typedef ::std::vector< InteractionInfo > InteractionList;
192 // member
193 private:
195 /// Used to create needed uno services at runtime.
196 css::uno::Reference< css::uno::XComponentContext > m_xContext;
198 /** The outside interaction handler, which is used to handle every incoming interaction,
199 if it's not blocked. */
200 css::uno::Reference< css::task::XInteractionHandler > m_xHandler;
202 std::unique_ptr<WarningDialogsParentScope> m_xWarningDialogsParent;
204 /** This list describe which and how incoming interactions must be handled.
205 Further it contains all collected information after this interaction
206 object was used.*/
207 InteractionList m_lInteractionRules;
210 // uno interface
211 public:
213 virtual void SAL_CALL initialize(const css::uno::Sequence<css::uno::Any>& rArguments) override;
216 @interface XInteractionHandler
217 @short called from outside to handle a problem
218 @descr We filter the incoming interactions. some of them
219 will be forwarded to the generic UI interaction handler.
220 So we must not implement it twice. Some other ones
221 will be aborted only.
223 @threadsafe yes
225 virtual void SAL_CALL handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest) override;
229 @interface XInteractionHandler2
230 @short called from outside to handle a problem
231 @descr We filter the incoming interactions. some of them
232 will be forwarded to the generic UI interaction handler.
233 So we must not implement it twice. Some other ones
234 will be aborted only.
236 @threadsafe yes
238 virtual sal_Bool SAL_CALL handleInteractionRequest( const css::uno::Reference< css::task::XInteractionRequest >& xRequest ) override;
242 @interface XInterface
243 @short called to query another interface of the component
244 @descr Will allow to query for XInteractionHandler2 if and only if m_xHandler supports this interface, too.
246 @threadsafe yes
248 virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type& aType ) override;
250 // c++ interface
251 public:
255 @short ctor to guarantee right initialized instances of this class
256 @descr It uses the given uno service manager to create the global
257 generic UI interaction handler for later internal using.
259 @param xSMGR
260 uno service manager for creating services internally
262 @threadsafe not necessary
264 PreventDuplicateInteraction(const css::uno::Reference< css::uno::XComponentContext >& rxContext);
268 @short dtor to free used memory.
270 virtual ~PreventDuplicateInteraction() override;
274 @short set the outside interaction handler, which must be used internally
275 if the interaction will not be blocked by the set list of rules.
277 @note This overwrites the settings of e.g. useDefaultUUIHandler()!
279 @param xHandler
280 the new interaction handler
282 void setHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler);
286 @short instead of setting an outside interaction handler, this method
287 make sure the default UUI interaction handler of the office is used.
289 @note This overwrites the settings of e.g. setHandler()!
291 void useDefaultUUIHandler();
295 @short add a new interaction to the list of interactions, which
296 must be handled by this helper.
298 @descr This method must be called immediately after a new instance of this helper was
299 created. Without such list of InteractionRules, this instances does nothing!
300 On the other side there is no possibility to remove rules.
301 So the same instance can't be used within different transactions.
302 It's a OneWay-object .-)
304 @param aInteractionInfo
305 describe the type of interaction, hos often it can be called etcpp.
307 @threadsafe yes
309 void addInteractionRule(const PreventDuplicateInteraction::InteractionInfo& aInteractionInfo);
313 @short return the info struct for the specified interaction.
315 @param aInteraction
316 specify the interaction.
318 @param pReturn
319 provides information about:
320 - the count how often this interaction was handled during the
321 lifetime of this helper.
322 - the interaction itself, so it can be analyzed further
324 @return [boolean]
325 true if the queried interaction could be found.
326 false otherwise.
328 @threadsafe yes
330 bool getInteractionInfo(const css::uno::Type& aInteraction,
331 PreventDuplicateInteraction::InteractionInfo* pReturn ) const;
334 } // namespace sfx2
336 #endif // INCLUDED_FRAMEWORK_PREVENTDUPLICATEINTERACTION_HXX
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */