1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26
27 #include "framework/preventduplicateinteraction.hxx"
28
29 //_________________________________________________________________________________________________________________
30 // my own includes
31 //_________________________________________________________________________________________________________________
32
33 //_________________________________________________________________________________________________________________
34 // interface includes
35 //_________________________________________________________________________________________________________________
36 #include <com/sun/star/task/XInteractionAbort.hpp>
37 #include <com/sun/star/task/XInteractionRetry.hpp>
38
39 //_________________________________________________________________________________________________________________
40 // other includes
41 //_________________________________________________________________________________________________________________
42
43 //_________________________________________________________________________________________________________________
44 // namespace
45 //_________________________________________________________________________________________________________________
46
47 namespace framework{
48
49 namespace css = ::com::sun::star;
50
51 //_________________________________________________________________________________________________________________
52 // exported const
53 //_________________________________________________________________________________________________________________
54
55 #define IMPLEMENTATIONNAME_UIINTERACTIONHANDLER ::rtl::OUString::createFromAscii("com.sun.star.comp.uui.UUIInteractionHandler")
56
57 //_________________________________________________________________________________________________________________
58 // exported definitions
59 //_________________________________________________________________________________________________________________
60
61 //_________________________________________________________________________________________________________________
62
PreventDuplicateInteraction(const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR)63 PreventDuplicateInteraction::PreventDuplicateInteraction(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
64 : ThreadHelpBase2()
65 , m_xSMGR(xSMGR)
66 {
67 }
68
69 //_________________________________________________________________________________________________________________
70
~PreventDuplicateInteraction()71 PreventDuplicateInteraction::~PreventDuplicateInteraction()
72 {
73 }
74
75 //_________________________________________________________________________________________________________________
76
setHandler(const css::uno::Reference<css::task::XInteractionHandler> & xHandler)77 void PreventDuplicateInteraction::setHandler(const css::uno::Reference< css::task::XInteractionHandler >& xHandler)
78 {
79 // SAFE ->
80 ::osl::ResettableMutexGuard aLock(m_aLock);
81 m_xHandler = xHandler;
82 aLock.clear();
83 // <- SAFE
84 }
85
86 //_________________________________________________________________________________________________________________
87
useDefaultUUIHandler()88 void PreventDuplicateInteraction::useDefaultUUIHandler()
89 {
90 // SAFE ->
91 ::osl::ResettableMutexGuard aLock(m_aLock);
92 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
93 aLock.clear();
94 // <- SAFE
95
96 css::uno::Reference< css::task::XInteractionHandler > xHandler(
97 xSMGR->createInstance(IMPLEMENTATIONNAME_UIINTERACTIONHANDLER),
98 css::uno::UNO_QUERY_THROW);
99
100 // SAFE ->
101 aLock.reset();
102 m_xHandler = xHandler;
103 aLock.clear();
104 // <- SAFE
105 }
106
107 //_________________________________________________________________________________________________________________
queryInterface(const css::uno::Type & aType)108 css::uno::Any SAL_CALL PreventDuplicateInteraction::queryInterface( const css::uno::Type& aType )
109 {
110 if ( aType.equals( XInteractionHandler2::static_type() ) )
111 {
112 ::osl::ResettableMutexGuard aLock(m_aLock);
113 css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
114 if ( !xHandler.is() )
115 return css::uno::Any();
116 }
117 return ::cppu::WeakImplHelper1< css::task::XInteractionHandler2 >::queryInterface( aType );
118 }
119
120 //_________________________________________________________________________________________________________________
121
handle(const css::uno::Reference<css::task::XInteractionRequest> & xRequest)122 void SAL_CALL PreventDuplicateInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
123 {
124 css::uno::Any aRequest = xRequest->getRequest();
125 sal_Bool bHandleIt = sal_True;
126
127 // SAFE ->
128 ::osl::ResettableMutexGuard aLock(m_aLock);
129
130 InteractionList::iterator pIt;
131 for ( pIt = m_lInteractionRules.begin();
132 pIt != m_lInteractionRules.end() ;
133 ++pIt )
134 {
135 InteractionInfo& rInfo = *pIt;
136
137 if (aRequest.isExtractableTo(rInfo.m_aInteraction))
138 {
139 ++rInfo.m_nCallCount;
140 rInfo.m_xRequest = xRequest;
141 bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
142 break;
143 }
144 }
145
146 css::uno::Reference< css::task::XInteractionHandler > xHandler = m_xHandler;
147
148 aLock.clear();
149 // <- SAFE
150
151 if (
152 (bHandleIt ) &&
153 (xHandler.is())
154 )
155 {
156 xHandler->handle(xRequest);
157 }
158 else
159 {
160 const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
161 sal_Int32 c = lContinuations.getLength();
162 sal_Int32 i = 0;
163 for (i=0; i<c; ++i)
164 {
165 css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
166 if (xAbort.is())
167 {
168 xAbort->select();
169 break;
170 }
171 }
172 }
173 }
174
175 //_________________________________________________________________________________________________________________
176
handleInteractionRequest(const css::uno::Reference<css::task::XInteractionRequest> & xRequest)177 ::sal_Bool SAL_CALL PreventDuplicateInteraction::handleInteractionRequest( const css::uno::Reference< css::task::XInteractionRequest >& xRequest )
178 {
179 css::uno::Any aRequest = xRequest->getRequest();
180 sal_Bool bHandleIt = sal_True;
181
182 // SAFE ->
183 ::osl::ResettableMutexGuard aLock(m_aLock);
184
185 InteractionList::iterator pIt;
186 for ( pIt = m_lInteractionRules.begin();
187 pIt != m_lInteractionRules.end() ;
188 ++pIt )
189 {
190 InteractionInfo& rInfo = *pIt;
191
192 if (aRequest.isExtractableTo(rInfo.m_aInteraction))
193 {
194 ++rInfo.m_nCallCount;
195 rInfo.m_xRequest = xRequest;
196 bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
197 break;
198 }
199 }
200
201 css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
202 OSL_ENSURE( xHandler.is() || !m_xHandler.is(),
203 "PreventDuplicateInteraction::handleInteractionRequest: inconsistency!" );
204
205 aLock.clear();
206 // <- SAFE
207
208 if (
209 (bHandleIt ) &&
210 (xHandler.is())
211 )
212 {
213 return xHandler->handleInteractionRequest(xRequest);
214 }
215 else
216 {
217 const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
218 sal_Int32 c = lContinuations.getLength();
219 sal_Int32 i = 0;
220 for (i=0; i<c; ++i)
221 {
222 css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
223 if (xAbort.is())
224 {
225 xAbort->select();
226 break;
227 }
228 }
229 }
230 return false;
231 }
232
233 //_________________________________________________________________________________________________________________
234
addInteractionRule(const PreventDuplicateInteraction::InteractionInfo & aInteractionInfo)235 void PreventDuplicateInteraction::addInteractionRule(const PreventDuplicateInteraction::InteractionInfo& aInteractionInfo)
236 {
237 // SAFE ->
238 ::osl::ResettableMutexGuard aLock(m_aLock);
239
240 InteractionList::iterator pIt;
241 for ( pIt = m_lInteractionRules.begin();
242 pIt != m_lInteractionRules.end() ;
243 ++pIt )
244 {
245 InteractionInfo& rInfo = *pIt;
246 if (rInfo.m_aInteraction == aInteractionInfo.m_aInteraction)
247 {
248 rInfo.m_nMaxCount = aInteractionInfo.m_nMaxCount ;
249 rInfo.m_nCallCount = aInteractionInfo.m_nCallCount;
250 return;
251 }
252 }
253
254 m_lInteractionRules.push_back(aInteractionInfo);
255
256 aLock.clear();
257 // <- SAFE
258 }
259
260 //_________________________________________________________________________________________________________________
261
getInteractionInfo(const css::uno::Type & aInteraction,PreventDuplicateInteraction::InteractionInfo * pReturn) const262 sal_Bool PreventDuplicateInteraction::getInteractionInfo(const css::uno::Type& aInteraction,
263 PreventDuplicateInteraction::InteractionInfo* pReturn ) const
264 {
265 // SAFE ->
266 ::osl::ResettableMutexGuard aLock(m_aLock);
267
268 PreventDuplicateInteraction::InteractionList::const_iterator pIt;
269 for ( pIt = m_lInteractionRules.begin();
270 pIt != m_lInteractionRules.end() ;
271 ++pIt )
272 {
273 const PreventDuplicateInteraction::InteractionInfo& rInfo = *pIt;
274 if (rInfo.m_aInteraction == aInteraction)
275 {
276 *pReturn = rInfo;
277 return sal_True;
278 }
279 }
280
281 aLock.clear();
282 // <- SAFE
283
284 return sal_False;
285 }
286
287 } // namespace framework
288