/src/libreoffice/sfx2/source/appl/preventduplicateinteraction.cxx
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 | | #include <preventduplicateinteraction.hxx> |
21 | | |
22 | | #include <comphelper/processfactory.hxx> |
23 | | #include <osl/diagnose.h> |
24 | | |
25 | | #include <com/sun/star/task/InteractionHandler.hpp> |
26 | | #include <com/sun/star/task/XInteractionAbort.hpp> |
27 | | #include <utility> |
28 | | |
29 | | namespace sfx2 { |
30 | | |
31 | | PreventDuplicateInteraction::PreventDuplicateInteraction(css::uno::Reference< css::uno::XComponentContext > xContext) |
32 | 0 | : m_xContext(std::move(xContext)) |
33 | 0 | { |
34 | 0 | } |
35 | | |
36 | | PreventDuplicateInteraction::~PreventDuplicateInteraction() |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | void PreventDuplicateInteraction::setHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler) |
41 | 0 | { |
42 | | // SAFE -> |
43 | 0 | std::unique_lock aLock(m_aLock); |
44 | 0 | m_xWarningDialogsParent.reset(); |
45 | 0 | m_xHandler = xHandler; |
46 | | // <- SAFE |
47 | 0 | } |
48 | | |
49 | | void PreventDuplicateInteraction::useDefaultUUIHandler() |
50 | 0 | { |
51 | | //if we use the default handler, set the parent to a window belonging to this object so that the dialogs |
52 | | //don't block unrelated windows. |
53 | 0 | m_xWarningDialogsParent.reset(new WarningDialogsParentScope(m_xContext)); |
54 | 0 | css::uno::Reference<css::task::XInteractionHandler> xHandler(css::task::InteractionHandler::createWithParent( |
55 | 0 | m_xContext, m_xWarningDialogsParent->GetDialogParent()), css::uno::UNO_QUERY_THROW); |
56 | | |
57 | | // SAFE -> |
58 | 0 | std::unique_lock aLock(m_aLock); |
59 | 0 | m_xHandler = std::move(xHandler); |
60 | | // <- SAFE |
61 | 0 | } |
62 | | |
63 | | css::uno::Any SAL_CALL PreventDuplicateInteraction::queryInterface( const css::uno::Type& aType ) |
64 | 0 | { |
65 | 0 | if ( aType.equals( cppu::UnoType<XInteractionHandler2>::get() ) ) |
66 | 0 | { |
67 | 0 | std::unique_lock aLock(m_aLock); |
68 | 0 | css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY ); |
69 | 0 | if ( !xHandler.is() ) |
70 | 0 | return css::uno::Any(); |
71 | 0 | } |
72 | 0 | return ::cppu::WeakImplHelper<css::lang::XInitialization, css::task::XInteractionHandler2>::queryInterface(aType); |
73 | 0 | } |
74 | | |
75 | | void SAL_CALL PreventDuplicateInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest) |
76 | 0 | { |
77 | 0 | css::uno::Any aRequest = xRequest->getRequest(); |
78 | 0 | bool bHandleIt = true; |
79 | | |
80 | | // SAFE -> |
81 | 0 | std::unique_lock aLock(m_aLock); |
82 | |
|
83 | 0 | auto pIt = std::find_if(m_lInteractionRules.begin(), m_lInteractionRules.end(), |
84 | 0 | [&aRequest](const InteractionInfo& rInfo) { return aRequest.isExtractableTo(rInfo.m_aInteraction); }); |
85 | 0 | if (pIt != m_lInteractionRules.end()) |
86 | 0 | { |
87 | 0 | InteractionInfo& rInfo = *pIt; |
88 | |
|
89 | 0 | ++rInfo.m_nCallCount; |
90 | 0 | rInfo.m_xRequest = xRequest; |
91 | 0 | bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount); |
92 | 0 | } |
93 | |
|
94 | 0 | css::uno::Reference< css::task::XInteractionHandler > xHandler = m_xHandler; |
95 | |
|
96 | 0 | aLock.unlock(); |
97 | | // <- SAFE |
98 | |
|
99 | 0 | if ( bHandleIt && xHandler.is() ) |
100 | 0 | { |
101 | 0 | xHandler->handle(xRequest); |
102 | 0 | } |
103 | 0 | else |
104 | 0 | { |
105 | 0 | const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations(); |
106 | 0 | for (const auto& rContinuation : lContinuations) |
107 | 0 | { |
108 | 0 | css::uno::Reference< css::task::XInteractionAbort > xAbort(rContinuation, css::uno::UNO_QUERY); |
109 | 0 | if (xAbort.is()) |
110 | 0 | { |
111 | 0 | xAbort->select(); |
112 | 0 | break; |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | sal_Bool SAL_CALL PreventDuplicateInteraction::handleInteractionRequest( const css::uno::Reference< css::task::XInteractionRequest >& xRequest ) |
119 | 0 | { |
120 | 0 | css::uno::Any aRequest = xRequest->getRequest(); |
121 | 0 | bool bHandleIt = true; |
122 | | |
123 | | // SAFE -> |
124 | 0 | std::unique_lock aLock(m_aLock); |
125 | |
|
126 | 0 | auto pIt = std::find_if(m_lInteractionRules.begin(), m_lInteractionRules.end(), |
127 | 0 | [&aRequest](const InteractionInfo& rInfo) { return aRequest.isExtractableTo(rInfo.m_aInteraction); }); |
128 | 0 | if (pIt != m_lInteractionRules.end()) |
129 | 0 | { |
130 | 0 | InteractionInfo& rInfo = *pIt; |
131 | |
|
132 | 0 | ++rInfo.m_nCallCount; |
133 | 0 | rInfo.m_xRequest = xRequest; |
134 | 0 | bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount); |
135 | 0 | } |
136 | |
|
137 | 0 | css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY ); |
138 | 0 | OSL_ENSURE( xHandler.is() || !m_xHandler.is(), |
139 | 0 | "PreventDuplicateInteraction::handleInteractionRequest: inconsistency!" ); |
140 | |
|
141 | 0 | aLock.unlock(); |
142 | | // <- SAFE |
143 | |
|
144 | 0 | if ( bHandleIt && xHandler.is() ) |
145 | 0 | { |
146 | 0 | return xHandler->handleInteractionRequest(xRequest); |
147 | 0 | } |
148 | 0 | else |
149 | 0 | { |
150 | 0 | const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations(); |
151 | 0 | for (const auto& rContinuation : lContinuations) |
152 | 0 | { |
153 | 0 | css::uno::Reference< css::task::XInteractionAbort > xAbort(rContinuation, css::uno::UNO_QUERY); |
154 | 0 | if (xAbort.is()) |
155 | 0 | { |
156 | 0 | xAbort->select(); |
157 | 0 | break; |
158 | 0 | } |
159 | 0 | } |
160 | 0 | } |
161 | 0 | return false; |
162 | 0 | } |
163 | | |
164 | | void PreventDuplicateInteraction::addInteractionRule(const PreventDuplicateInteraction::InteractionInfo& aInteractionInfo) |
165 | 0 | { |
166 | | // SAFE -> |
167 | 0 | std::unique_lock aLock(m_aLock); |
168 | |
|
169 | 0 | auto pIt = std::find_if(m_lInteractionRules.begin(), m_lInteractionRules.end(), |
170 | 0 | [&aInteractionInfo](const InteractionInfo& rInfo) { return rInfo.m_aInteraction == aInteractionInfo.m_aInteraction; }); |
171 | 0 | if (pIt != m_lInteractionRules.end()) |
172 | 0 | { |
173 | 0 | InteractionInfo& rInfo = *pIt; |
174 | 0 | rInfo.m_nMaxCount = aInteractionInfo.m_nMaxCount; |
175 | 0 | rInfo.m_nCallCount = aInteractionInfo.m_nCallCount; |
176 | 0 | return; |
177 | 0 | } |
178 | | |
179 | 0 | m_lInteractionRules.push_back(aInteractionInfo); |
180 | | // <- SAFE |
181 | 0 | } |
182 | | |
183 | | bool PreventDuplicateInteraction::getInteractionInfo(const css::uno::Type& aInteraction, |
184 | | PreventDuplicateInteraction::InteractionInfo* pReturn ) const |
185 | 0 | { |
186 | | // SAFE -> |
187 | 0 | std::unique_lock aLock(m_aLock); |
188 | |
|
189 | 0 | auto pIt = std::find_if(m_lInteractionRules.begin(), m_lInteractionRules.end(), |
190 | 0 | [&aInteraction](const InteractionInfo& rInfo) { return rInfo.m_aInteraction == aInteraction; }); |
191 | 0 | if (pIt != m_lInteractionRules.end()) |
192 | 0 | { |
193 | 0 | *pReturn = *pIt; |
194 | 0 | return true; |
195 | 0 | } |
196 | | // <- SAFE |
197 | | |
198 | 0 | return false; |
199 | 0 | } |
200 | | |
201 | | void SAL_CALL PreventDuplicateInteraction::initialize(const css::uno::Sequence<css::uno::Any>& rArguments) |
202 | 0 | { |
203 | 0 | std::unique_lock aLock(m_aLock); |
204 | | // If we're re-initialized to set a specific new window as a parent then drop our temporary |
205 | | // dialog parent |
206 | 0 | css::uno::Reference<css::lang::XInitialization> xHandler(m_xHandler, css::uno::UNO_QUERY); |
207 | 0 | if (xHandler.is()) |
208 | 0 | { |
209 | 0 | m_xWarningDialogsParent.reset(); |
210 | 0 | xHandler->initialize(rArguments); |
211 | 0 | } |
212 | 0 | } |
213 | | |
214 | | IMPL_STATIC_LINK_NOARG(WarningDialogsParent, TerminateDesktop, void*, void) |
215 | 0 | { |
216 | 0 | css::frame::Desktop::create(comphelper::getProcessComponentContext())->terminate(); |
217 | 0 | } |
218 | | |
219 | | } // namespace sfx2 |
220 | | |
221 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |