Line data Source code
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 <framework/preventduplicateinteraction.hxx>
21 :
22 : #include <com/sun/star/task/InteractionHandler.hpp>
23 : #include <com/sun/star/task/XInteractionAbort.hpp>
24 : #include <com/sun/star/task/XInteractionRetry.hpp>
25 :
26 : namespace framework{
27 :
28 0 : PreventDuplicateInteraction::PreventDuplicateInteraction(const css::uno::Reference< css::uno::XComponentContext >& rxContext)
29 : : ThreadHelpBase2()
30 0 : , m_xContext(rxContext)
31 : {
32 0 : }
33 :
34 0 : PreventDuplicateInteraction::~PreventDuplicateInteraction()
35 : {
36 0 : }
37 :
38 0 : void PreventDuplicateInteraction::setHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler)
39 : {
40 : // SAFE ->
41 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
42 0 : m_xHandler = xHandler;
43 0 : aLock.clear();
44 : // <- SAFE
45 0 : }
46 :
47 0 : void PreventDuplicateInteraction::useDefaultUUIHandler()
48 : {
49 : // SAFE ->
50 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
51 0 : aLock.clear();
52 : // <- SAFE
53 :
54 0 : css::uno::Reference< css::task::XInteractionHandler > xHandler( css::task::InteractionHandler::createWithParent( m_xContext, 0 ), css::uno::UNO_QUERY_THROW );
55 :
56 : // SAFE ->
57 0 : aLock.reset();
58 0 : m_xHandler = xHandler;
59 0 : aLock.clear();
60 : // <- SAFE
61 0 : }
62 :
63 0 : css::uno::Any SAL_CALL PreventDuplicateInteraction::queryInterface( const css::uno::Type& aType )
64 : throw (css::uno::RuntimeException, std::exception)
65 : {
66 0 : if ( aType.equals( cppu::UnoType<XInteractionHandler2>::get() ) )
67 : {
68 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
69 0 : css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
70 0 : if ( !xHandler.is() )
71 0 : return css::uno::Any();
72 : }
73 0 : return ::cppu::WeakImplHelper1< css::task::XInteractionHandler2 >::queryInterface( aType );
74 : }
75 :
76 0 : void SAL_CALL PreventDuplicateInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
77 : throw(css::uno::RuntimeException, std::exception)
78 : {
79 0 : css::uno::Any aRequest = xRequest->getRequest();
80 0 : bool bHandleIt = true;
81 :
82 : // SAFE ->
83 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
84 :
85 0 : InteractionList::iterator pIt;
86 0 : for ( pIt = m_lInteractionRules.begin();
87 0 : pIt != m_lInteractionRules.end();
88 : ++pIt )
89 : {
90 0 : InteractionInfo& rInfo = *pIt;
91 :
92 0 : if (aRequest.isExtractableTo(rInfo.m_aInteraction))
93 : {
94 0 : ++rInfo.m_nCallCount;
95 0 : rInfo.m_xRequest = xRequest;
96 0 : bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
97 0 : break;
98 : }
99 : }
100 :
101 0 : css::uno::Reference< css::task::XInteractionHandler > xHandler = m_xHandler;
102 :
103 0 : aLock.clear();
104 : // <- SAFE
105 :
106 0 : if (
107 0 : (bHandleIt ) &&
108 0 : (xHandler.is())
109 : )
110 : {
111 0 : xHandler->handle(xRequest);
112 : }
113 : else
114 : {
115 0 : const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
116 0 : sal_Int32 c = lContinuations.getLength();
117 0 : sal_Int32 i = 0;
118 0 : for (i=0; i<c; ++i)
119 : {
120 0 : css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
121 0 : if (xAbort.is())
122 : {
123 0 : xAbort->select();
124 0 : break;
125 : }
126 0 : }
127 0 : }
128 0 : }
129 :
130 0 : sal_Bool SAL_CALL PreventDuplicateInteraction::handleInteractionRequest( const css::uno::Reference< css::task::XInteractionRequest >& xRequest )
131 : throw (css::uno::RuntimeException, std::exception)
132 : {
133 0 : css::uno::Any aRequest = xRequest->getRequest();
134 0 : bool bHandleIt = true;
135 :
136 : // SAFE ->
137 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
138 :
139 0 : InteractionList::iterator pIt;
140 0 : for ( pIt = m_lInteractionRules.begin();
141 0 : pIt != m_lInteractionRules.end();
142 : ++pIt )
143 : {
144 0 : InteractionInfo& rInfo = *pIt;
145 :
146 0 : if (aRequest.isExtractableTo(rInfo.m_aInteraction))
147 : {
148 0 : ++rInfo.m_nCallCount;
149 0 : rInfo.m_xRequest = xRequest;
150 0 : bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
151 0 : break;
152 : }
153 : }
154 :
155 0 : css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
156 : OSL_ENSURE( xHandler.is() || !m_xHandler.is(),
157 : "PreventDuplicateInteraction::handleInteractionRequest: inconsistency!" );
158 :
159 0 : aLock.clear();
160 : // <- SAFE
161 :
162 0 : if (
163 0 : (bHandleIt ) &&
164 0 : (xHandler.is())
165 : )
166 : {
167 0 : return xHandler->handleInteractionRequest(xRequest);
168 : }
169 : else
170 : {
171 0 : const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
172 0 : sal_Int32 c = lContinuations.getLength();
173 0 : sal_Int32 i = 0;
174 0 : for (i=0; i<c; ++i)
175 : {
176 0 : css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
177 0 : if (xAbort.is())
178 : {
179 0 : xAbort->select();
180 0 : break;
181 : }
182 0 : }
183 : }
184 0 : return false;
185 : }
186 :
187 0 : void PreventDuplicateInteraction::addInteractionRule(const PreventDuplicateInteraction::InteractionInfo& aInteractionInfo)
188 : {
189 : // SAFE ->
190 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
191 :
192 0 : InteractionList::iterator pIt;
193 0 : for ( pIt = m_lInteractionRules.begin();
194 0 : pIt != m_lInteractionRules.end();
195 : ++pIt )
196 : {
197 0 : InteractionInfo& rInfo = *pIt;
198 0 : if (rInfo.m_aInteraction == aInteractionInfo.m_aInteraction)
199 : {
200 0 : rInfo.m_nMaxCount = aInteractionInfo.m_nMaxCount;
201 0 : rInfo.m_nCallCount = aInteractionInfo.m_nCallCount;
202 0 : return;
203 : }
204 : }
205 :
206 0 : m_lInteractionRules.push_back(aInteractionInfo);
207 :
208 0 : aLock.clear();
209 : // <- SAFE
210 : }
211 :
212 0 : bool PreventDuplicateInteraction::getInteractionInfo(const css::uno::Type& aInteraction,
213 : PreventDuplicateInteraction::InteractionInfo* pReturn ) const
214 : {
215 : // SAFE ->
216 0 : ::osl::ResettableMutexGuard aLock(m_aLock);
217 :
218 0 : PreventDuplicateInteraction::InteractionList::const_iterator pIt;
219 0 : for ( pIt = m_lInteractionRules.begin();
220 0 : pIt != m_lInteractionRules.end();
221 : ++pIt )
222 : {
223 0 : const PreventDuplicateInteraction::InteractionInfo& rInfo = *pIt;
224 0 : if (rInfo.m_aInteraction == aInteraction)
225 : {
226 0 : *pReturn = rInfo;
227 0 : return true;
228 : }
229 : }
230 :
231 0 : aLock.clear();
232 : // <- SAFE
233 :
234 0 : return false;
235 : }
236 :
237 : } // namespace framework
238 :
239 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|