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     throw (css::uno::RuntimeException)
110 {
111     if ( aType.equals( XInteractionHandler2::static_type() ) )
112     {
113         ::osl::ResettableMutexGuard aLock(m_aLock);
114         css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
115         if ( !xHandler.is() )
116             return css::uno::Any();
117     }
118     return ::cppu::WeakImplHelper1< css::task::XInteractionHandler2 >::queryInterface( aType );
119 }
120 
121 //_________________________________________________________________________________________________________________
122 
handle(const css::uno::Reference<css::task::XInteractionRequest> & xRequest)123 void SAL_CALL PreventDuplicateInteraction::handle(const css::uno::Reference< css::task::XInteractionRequest >& xRequest)
124     throw(css::uno::RuntimeException)
125 {
126     css::uno::Any aRequest  = xRequest->getRequest();
127     sal_Bool      bHandleIt = sal_True;
128 
129     // SAFE ->
130     ::osl::ResettableMutexGuard aLock(m_aLock);
131 
132     InteractionList::iterator pIt;
133     for (  pIt  = m_lInteractionRules.begin();
134            pIt != m_lInteractionRules.end()  ;
135          ++pIt                               )
136     {
137         InteractionInfo& rInfo = *pIt;
138 
139         if (aRequest.isExtractableTo(rInfo.m_aInteraction))
140         {
141             ++rInfo.m_nCallCount;
142             rInfo.m_xRequest = xRequest;
143             bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
144             break;
145         }
146     }
147 
148     css::uno::Reference< css::task::XInteractionHandler > xHandler = m_xHandler;
149 
150     aLock.clear();
151     // <- SAFE
152 
153     if (
154         (bHandleIt    ) &&
155         (xHandler.is())
156        )
157     {
158         xHandler->handle(xRequest);
159     }
160     else
161     {
162         const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
163         sal_Int32 c = lContinuations.getLength();
164         sal_Int32 i = 0;
165         for (i=0; i<c; ++i)
166         {
167             css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
168             if (xAbort.is())
169             {
170                 xAbort->select();
171                 break;
172             }
173         }
174     }
175 }
176 
177 //_________________________________________________________________________________________________________________
178 
handleInteractionRequest(const css::uno::Reference<css::task::XInteractionRequest> & xRequest)179 ::sal_Bool SAL_CALL PreventDuplicateInteraction::handleInteractionRequest( const css::uno::Reference< css::task::XInteractionRequest >& xRequest )
180             throw (css::uno::RuntimeException)
181 {
182     css::uno::Any aRequest  = xRequest->getRequest();
183     sal_Bool      bHandleIt = sal_True;
184 
185     // SAFE ->
186     ::osl::ResettableMutexGuard aLock(m_aLock);
187 
188     InteractionList::iterator pIt;
189     for (  pIt  = m_lInteractionRules.begin();
190            pIt != m_lInteractionRules.end()  ;
191          ++pIt                               )
192     {
193         InteractionInfo& rInfo = *pIt;
194 
195         if (aRequest.isExtractableTo(rInfo.m_aInteraction))
196         {
197             ++rInfo.m_nCallCount;
198             rInfo.m_xRequest = xRequest;
199             bHandleIt = (rInfo.m_nCallCount <= rInfo.m_nMaxCount);
200             break;
201         }
202     }
203 
204     css::uno::Reference< css::task::XInteractionHandler2 > xHandler( m_xHandler, css::uno::UNO_QUERY );
205     OSL_ENSURE( xHandler.is() || !m_xHandler.is(),
206         "PreventDuplicateInteraction::handleInteractionRequest: inconsistency!" );
207 
208     aLock.clear();
209     // <- SAFE
210 
211     if (
212         (bHandleIt    ) &&
213         (xHandler.is())
214        )
215     {
216         return xHandler->handleInteractionRequest(xRequest);
217     }
218     else
219     {
220         const css::uno::Sequence< css::uno::Reference< css::task::XInteractionContinuation > > lContinuations = xRequest->getContinuations();
221         sal_Int32 c = lContinuations.getLength();
222         sal_Int32 i = 0;
223         for (i=0; i<c; ++i)
224         {
225             css::uno::Reference< css::task::XInteractionAbort > xAbort(lContinuations[i], css::uno::UNO_QUERY);
226             if (xAbort.is())
227             {
228                 xAbort->select();
229                 break;
230             }
231         }
232     }
233     return false;
234 }
235 
236 //_________________________________________________________________________________________________________________
237 
addInteractionRule(const PreventDuplicateInteraction::InteractionInfo & aInteractionInfo)238 void PreventDuplicateInteraction::addInteractionRule(const PreventDuplicateInteraction::InteractionInfo& aInteractionInfo)
239 {
240     // SAFE ->
241     ::osl::ResettableMutexGuard aLock(m_aLock);
242 
243     InteractionList::iterator pIt;
244     for (  pIt  = m_lInteractionRules.begin();
245            pIt != m_lInteractionRules.end()  ;
246          ++pIt                               )
247     {
248         InteractionInfo& rInfo = *pIt;
249         if (rInfo.m_aInteraction == aInteractionInfo.m_aInteraction)
250         {
251             rInfo.m_nMaxCount  = aInteractionInfo.m_nMaxCount ;
252             rInfo.m_nCallCount = aInteractionInfo.m_nCallCount;
253             return;
254         }
255     }
256 
257     m_lInteractionRules.push_back(aInteractionInfo);
258 
259     aLock.clear();
260     // <- SAFE
261 }
262 
263 //_________________________________________________________________________________________________________________
264 
getInteractionInfo(const css::uno::Type & aInteraction,PreventDuplicateInteraction::InteractionInfo * pReturn) const265 sal_Bool PreventDuplicateInteraction::getInteractionInfo(const css::uno::Type&                               aInteraction,
266                                                                PreventDuplicateInteraction::InteractionInfo* pReturn     ) const
267 {
268     // SAFE ->
269     ::osl::ResettableMutexGuard aLock(m_aLock);
270 
271     PreventDuplicateInteraction::InteractionList::const_iterator pIt;
272     for (  pIt  = m_lInteractionRules.begin();
273            pIt != m_lInteractionRules.end()  ;
274          ++pIt                               )
275     {
276         const PreventDuplicateInteraction::InteractionInfo& rInfo = *pIt;
277         if (rInfo.m_aInteraction == aInteraction)
278         {
279             *pReturn = rInfo;
280             return sal_True;
281         }
282     }
283 
284     aLock.clear();
285     // <- SAFE
286 
287     return sal_False;
288 }
289 
290 } // namespace framework
291