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